Android:相对布局内容低于屏幕

Neo*_*Neo 1 android android-layout android-fragments

我是android的新手,我只是想创建一个聊天客户端.然而,底部的内容只是在屏幕外.

布局文件:

chat_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e1e1e1"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/form"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:background="#91f1f1f1"
    android:orientation="horizontal"
    android:paddingBottom="2dp" >

    <ImageButton
        android:id="@+id/sendMessageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/send_button"
        android:text="d" />
</LinearLayout>


</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

activity_mail.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.marothiatechs.mchat.MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />


</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

这在Android Studio的预览窗口中看起来不错. Android Studio预览版

但是,在我的设备上,图像按钮不可见. 实际设备

但是当我将linearlayout的"paddingBottom"增加到"200dp"时,按钮变得可见.实际设备 - 底部填充为200dp

我在SO上发现了类似的问题,其中大多数建议使用ScrollView.但我不认为ScrollView在我的情况下是必要的.有任何想法吗?

Sab*_*ari 9

试试这个-

在LinearLayout中使用AppBarLayout和ViewPager.

  <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.marothiatechs.mchat.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

  • 但``layout_scrollFlags`到`Toolbar`不起作用. (4认同)
  • (我的英文不好,请调整).CoordinatorLayout是父视图,它有2个子视图(AppBarLayout和ViewPager).你将viewpager高度设置为matchparent.所以它的高度将等于CoordinatorLayout高度.例如:CoordinatorLayout高度为500dp,现在viewpager高度为500dp,AppBarLayout高度为"x".现在总高度是(500 + x).这就是隐藏图像的原因.现在,在更新的xml中,CoordinatorLayout只有一个子节点(LinearLayout).LinearLoyout有2个子节点,因此它将使用其父布局高度进行调整.我希望你明白了...... (2认同)