【问题标题】:Layout issue between Relative Layout and Coordinator Layout相对布局和协调器布局之间的布局问题
【发布时间】:2015-12-09 23:21:58
【问题描述】:

谁能帮我理解相对布局和协调器布局之间的布局问题。最初我在相对布局中使用浮动操作按钮。下面是代码。

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

输出:

为了使用 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>

输出:

无论如何,我通过将 android:layout_alignParentRight 和 android:layout_alignParentBottom 替换为 android:layoutGravity="right|bottom" 解决了这个问题。

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

感谢您的回答。谢谢。

【问题讨论】:

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


    【解决方案1】:

    你应该使用android:gravity="bottom|right"layout_alignxxxx只适用于RelativeLayout以下的孩子

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

    【讨论】:

      猜你喜欢
      • 2016-04-21
      • 1970-01-01
      • 2018-06-29
      • 2015-11-02
      • 1970-01-01
      • 2015-12-19
      • 1970-01-01
      相关资源
      最近更新 更多