在协调器布局中排列视图

时间:2018-06-20 14:22:37

标签: android view coordinator-layout

我在将“框架”布局设置在“底部导航抽屉”下方时遇到问题(是的,我将其放在顶部:)。现在,BND隐藏了帧布局的顶部,因为它与BND一样与父顶部对齐,而不是与BND底部对齐。

代码如下:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/coordID">

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/BND_ID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/m_navigation"
        />

    <FrameLayout
        android:id="@+id/fID2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </FrameLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/dummyFAB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="16dp"
        app:srcCompat="@drawable/ic_settings"
        app:layout_insetEdge="bottom" />

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

2 个答案:

答案 0 :(得分:2)

您应尝试将它们包装在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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/coordID">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <android.support.design.widget.BottomNavigationView
            android:id="@+id/BND_ID"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/windowBackground"
            app:menu="@menu/m_navigation" />

        <FrameLayout
            android:id="@+id/fID2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/BND_ID"/>

    </RelativeLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/dummyFAB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_gravity="end|bottom"
        app:layout_anchor="@+id/relativeLayout"
        app:layout_anchorGravity="right|bottom"
        app:layout_insetEdge="bottom"
        app:srcCompat="@drawable/ic_settings" />



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

答案 1 :(得分:0)

CoordinatorLayout只是docs中所述的超强FrameLayout。

这就是视图重叠的原因。除非您要使用此视图组提供的任何行为,否则建议您更改为其他布局设置,例如

 <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="match_parent">

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/BND_ID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/m_navigation" />

    <FrameLayout
        android:id="@+id/fID2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/BND_ID">
    </FrameLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/dummyFAB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_gravity="end|bottom"
        app:layout_insetEdge="bottom"
        app:srcCompat="@drawable/ic_settings" />


</RelativeLayout>

或利用其中的coordinatorLayout行为之一

例如

<?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:layout_width="match_parent"
   android:layout_height="match_parent"
   android:id="@+id/coordID">

<android.support.design.widget.BottomNavigationView
    android:id="@+id/BND_ID"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/windowBackground"
    //Add the line below
    app:layout_scrollFlags="scroll|enterAlways"
    app:menu="@menu/m_navigation"/>

<FrameLayout
    android:id="@+id/fID2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    //Add the line below
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
</FrameLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/dummyFAB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="16dp"
    app:srcCompat="@drawable/ic_settings"
    app:layout_insetEdge="bottom" />

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

因此,当您将任何内容放置在frameLayout中时,您的底部导航栏都将隐藏。

相关问题