2017-08-20 51 views
-3

我有三個按鈕需要位於屏幕底部的一行。以下是活動xml中的代碼。爲了使三個按鈕佔用空間,我將它們中的每一個都包含在LinearLayout中,並將佈局android:layout_weight設置爲1和android:layout_gravity爲中心。如何佈置三個均勻位於LinearLayout下的按鈕?

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="90px" 
     android:background="#88104502" 
     android:orientation="horizontal"> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" 
      android:orientation="horizontal" 
      android:layout_weight="1"> 
      <Button 
       android:id="@+id/goBack" 
       android:layout_width="70px" 
       android:layout_height="80px" 
       android:gravity="center" 
       android:background="@drawable/back" 
       android:onClick="toHomePage" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" 
      android:orientation="horizontal" 
      android:layout_weight="1"> 
      <Button 
       android:id="@+id/findPlant" 
       android:layout_width="70px" 
       android:layout_height="80px" 
       android:gravity="center" 
       android:background="@drawable/flowr" 
       android:onClick="toImagePage" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      android:layout_gravity="center" 
      android:layout_weight="1"> 

      <Button 
       android:id="@+id/plantList" 
       android:layout_width="70px" 
       android:layout_height="80px" 
       android:gravity="center" 
       android:background="@drawable/list" /> 
     </LinearLayout> 
    </LinearLayout> 

但它的按鈕並不位於LinearLayout的中心。下面是截圖:

enter image description here

你可以看到每個按鍵位於的LinearLayout的左側。如何讓他們中心位於?

+1

爲什麼你使用三種不同的線性佈局?它也使用一個主LinearLayout完成,並給予3個按鈕相同的權重。 –

回答

0

試試這個。我使用你的代碼只給線性佈局的重力。如果你不想使用這麼多的LinearLayout只使用一個主要的LinearLayout,並給予3個按鈕同樣的權重。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    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="90px" 
    android:background="#88104502" 
    android:orientation="horizontal"> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:orientation="horizontal" 
    android:gravity="center" 
    android:layout_weight="1"> 
    <Button 
     android:id="@+id/goBack" 
     android:layout_width="70px" 
     android:layout_height="80px" 
     android:gravity="center" 
     android:background="@mipmap/ic_launcher" 
     android:onClick="toHomePage" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:gravity="center" 
    android:orientation="horizontal" 
    android:layout_weight="1"> 
    <Button 
     android:id="@+id/findPlant" 
     android:layout_width="70px" 
     android:layout_height="80px" 
     android:gravity="center" 
     android:background="@mipmap/ic_launcher" 
     android:onClick="toImagePage" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:layout_gravity="center" 
    android:gravity="center" 
    android:layout_weight="1"> 

    <Button 
     android:id="@+id/plantList" 
     android:layout_width="70px" 
     android:layout_height="80px" 
     android:background="@mipmap/ic_launcher" /> 
</LinearLayout> 

0

您可以創建一個空視圖作爲填充。這樣,即使圖標寬度不同,間距也將始終保持不變。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="90px" 
    android:background="#88104502" 
    android:orientation="horizontal"> 

     <View 
      android:layout_weight="1"      
      android:layout_width="0dp" 
      android:layout_height="0dp"/> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="70px" 
      android:layout_height="80px" 
      android:background="@drawable/list" /> 
     <View 
      android:layout_weight="1"      
      android:layout_width="0dp" 
      android:layout_height="0dp"/> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="70px" 
      android:layout_height="80px" 
      android:background="@drawable/list" /> 
     <View 
      android:layout_weight="1"      
      android:layout_width="0dp" 
      android:layout_height="0dp"/> 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="70px" 
      android:layout_height="80px" 
      android:background="@drawable/list" /> 
     <View 
      android:layout_weight="1"      
      android:layout_width="0dp" 
      android:layout_height="0dp"/> 
</LinearLayout> 
1

如果你想與圖像的按鈕,你可以使用的ImageButton代替按鈕,你可以嘗試這樣的,而是使用嵌套的佈局,可以直接給權重的ImageButton。

如果您想要設置一個按鈕的樣式,您可以使用AppCompatImageButton,因爲您可以使用app:backgroundTint來更改背景。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="90px" 
    android:background="#88104502" 
    android:weightSum="3" 
    android:orientation="horizontal"> 

    <ImageButton 
     android:id="@+id/goBack" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:onClick="toHomePage" 
     android:src="@drawable/ic_action_name"/> 

    <ImageButton 
     android:id="@+id/findPlant" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:onClick="toImagePage" 
     android:src="@drawable/ic_action_name"/> 

    <ImageButton 
     android:id="@+id/plantList" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:onClick="toImagePage" 
     android:src="@drawable/ic_action_name"/> 


</LinearLayout> 
+0

這是所有答案中**最乾淨的**(也是**最快的**)佈局。保持**簡單:表演**是相關的。 +1 –

0

利用重力而非layout_gravity。