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