【问题标题】:Include a new layout, inside CoordinatorLayout not working properly?包括一个新的布局,在 CoordinatorLayout 内不能正常工作?
【发布时间】:2016-02-25 12:14:40
【问题描述】:

我有两种布局。第一个布局文件名为home.xml,第二个名为coordinator.xml。所以我只想在 coordinator.xml 中包含home.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>

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>

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

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

【问题讨论】:

    标签: android android-layout android-toolbar android-coordinatorlayout


    【解决方案1】:

    包括这样的布局:

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

    只需在您想要位于 AppbarLayout 下的布局中使用 app:layout_behavior="@string/appbar_scrolling_view_behavior" 即可:

    <?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>
    

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

    <?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>
    

    编译后:

    对于这种重叠,我们需要查看actionbar_layout,但目前,上述布局应该可以工作。

    【讨论】:

    • 感谢您的回答,但您的解决方案对我不起作用。
    • @DevendraDagur - actionbar_layout 的内容是什么?尝试更新的答案,它现在应该可以工作了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-11
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 2022-07-08
    相关资源
    最近更新 更多