0

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

<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

中序与小吃吧工作i的相同的代码用于协调布局。但我得到了浮动行动栏在不同的位置。

<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

无论如何,我通过更换机器人解决该问题:layout_alignParentRight和android:layout_alignParentBottom与Android:layoutGravity = “右|底”。

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

欣赏你的答案。谢谢。

回答

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"> 
下工作的儿童