相对布局和协调器布局之间的布局问题

时间:2015-09-14 02:51:00

标签: android material-design floating-action-button android-coordinatorlayout

有人可以帮助我理解相对布局和协调器布局之间的布局问题。最初,我在相对布局中使用了浮动操作按钮。下面是代码。

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/primary_color"></android.support.v7.widget.Toolbar>


        <android.support.design.widget.FloatingActionButton
            android:id="@+id/searchfab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="20dp"
            android:layout_marginRight="20dp"
            android:src="@drawable/ic_add_black_24dp"
            app:fabSize="normal">

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

</RelativeLayout>

输出:

enter image description here

为了使用Snackbar我在同一代码中使用了Coordinator布局。但我的浮动动作栏位于不同的位置。

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary_color"></android.support.v7.widget.Toolbar>

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/searchfab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="20dp"
            android:layout_marginRight="20dp"
            android:src="@drawable/ic_add_black_24dp"
            app:fabSize="normal">

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

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

输出:

enter image description here

无论如何我通过用android替换android:layout_alignParentRight和android:layout_alignParentBottom来解决这个问题:layoutGravity =“right | bottom”。

问题是为什么android:layout_alignParentRight和android:layout_alignParentBottom不适用于协调器布局,尽管它的高度和宽度保持为match_parent。

感谢您的回答。谢谢。

0 个答案:

没有答案