在协调布局中安排视图

6
我遇到了一个问题,希望能够将我的Frame布局放在底部导航抽屉的下方(是的,我把它放在了顶部 :))。现在,由于与父类的顶部对齐,而不是与底部导航抽屉对齐,因此Frame布局的顶部被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>

为什么要使用协调布局?协调布局不是通过相对位置来定位视图,而是使用行为。除非您计划具有滚动行为,否则最好将视图包装在一起或使用约束布局。 - dzsonni
好的,我将要吃小吃,当 Snackbar 被调用时,我需要 FAB 上移。我不知道如何在相对布局中实现它,这是在我更改它之前的原始布局。 - Nemanja
只需将工具栏和帧布局放入一个相对布局中,而将浮动操作按钮排除在外。为相对布局和浮动操作按钮分别设置下面的行为,这样您就会有一个可移动的浮动操作按钮,但内容固定不变。我建议不要移动实际内容,以防止浮动操作按钮出现问题。 - dzsonni
@dzsonni 好的,我应该添加什么行为?我以前没有使用过CoorLayout,所以不知道。我需要FAB在右下角。 - Nemanja
请在 FAB 中添加以下代码: app:layout_anchor="@id/fID2" 和 app:layout_anchorGravity="bottom|right|end"。 - dzsonni
2个回答

7
你应该尝试将它们包裹在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>

我尝试了那样做,但是当我调用snackbar时,FAB没有上移。 - Nemanja
@Nemanja 我的 fab 按钮与父布局底部对齐,因此它不会移动。你可以将其移除,或者我建议将 fab 按钮放在 relativeLayout 之外。 - Umair
好的,如果我把 FAB 放在外面,怎么让它处于右下角呢? - Nemanja
@Nemanja 是的。我已经更新了我的答案,请看一下。 - Umair
1
谢谢,将FAB从RL移动到CoorL之外解决了问题。甚至不需要那些“anchor”行。我只是太累了,试图修复它,我的头脑已经崩溃了:) 再次感谢 - Nemanja

2

CoordinatorLayout只是一个超级强大的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中放置任何内容进行滚动时,底部导航栏会隐藏。

谢谢你的努力,非常感激。我通过下面的答案解决了这个问题。 - Nemanja

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接