包括一个新的布局,里面 CoordinatorLayout 不能正常工作?

Dev*_*gur 2 android android-layout android-toolbar android-coordinatorlayout

我有两种布局。第一个布局文件名是home.xml,第二个名称是coordinator.xml. 所以我只想包含home.xml在 coordinator.xml 中。但问题是当我尝试将此布局包含在 中时coordinator.xml,它会穿过移动屏幕并进入其中。

如果我包含app:layout_behavior="@string/appbar_scrolling_view_behavior"在我的包含布局中,它会穿过移动屏幕并进入其中。

如果我不包含此行为,它会与Toolbar.

那么解决方案是什么?

home.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:id="@+id/view"
        android:layout_above="@+id/linear1"
        android:background="#00409f"
        android:layout_marginBottom="10dp"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:id="@+id/linear1"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:src="@drawable/home"/>
            <TextView
                android:layout_width="wrap_content"
                android:textColor="#000000"
                android:layout_height="wrap_content"
                android:text="Home"/>

        </LinearLayout>

        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:src="@drawable/wallet"/>
            <TextView
                android:layout_width="wrap_content"
                android:textColor="#000000"
                android:layout_height="wrap_content"
                android:text="Wallet"/>

        </LinearLayout>
        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:src="@drawable/qr_code"/>
            <TextView
                android:layout_width="wrap_content"
                android:textColor="#000000"
                android:layout_height="wrap_content"
                android:text="QR Code"/>

        </LinearLayout>
        <LinearLayout
            android:layout_weight="1"
            android:layout_width="0dp"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:src="@drawable/user_male"/>
            <TextView
                android:layout_width="wrap_content"
                android:textColor="#000000"
                android:layout_height="wrap_content"
                android:text="Profile"/>

        </LinearLayout>

    </LinearLayout>

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

coordinator.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"
    android:clickable="true"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    tools:context=".Home">


    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent" 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:contentInsetStart="0dp"
            android:contentInsetLeft="0dp"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay">
            <include layout="@layout/actionbar_layout"/>

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

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


    <!--Include layout here give me problem-->

    <include layout="@layout/home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"

        />

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

有了app:layout_behavior="@string/appbar_scrolling_view_behavior" 它看起来就像这样:

问题图片

没有app:layout_behavior="@string/appbar_scrolling_view_behavior" 它看起来像这样:

问题 img

ʍѳђ*_*ઽ૯ท 6

包括这样的布局:

<include layout="@layout/home" />
Run Code Online (Sandbox Code Playgroud)

只需app:layout_behavior="@string/appbar_scrolling_view_behavior"在您想要在这样的布局中使用它AppbarLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World" />

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_above="@+id/linear1"
        android:layout_marginBottom="10dp"
        android:background="#00409f" />

    <LinearLayout
        android:id="@+id/linear1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Home"
                android:textColor="#000000" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Wallet"
                android:textColor="#000000" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="QR Code"
                android:textColor="#000000" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Profile"
                android:textColor="#000000" />

        </LinearLayout>

    </LinearLayout>

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

更新:我刚刚再次检查,这是另一个布局:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:fitsSystemWindows="true"
    tools:context=".Home">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:popupTheme="@style/AppTheme">

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

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


    <include layout="@layout/home" />

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

编译后:

在此处输入图片说明

对于重叠,我们需要看到,actionbar_layout但现在,上面的布局应该可以工作。