2012-07-26 52 views
34

我想要三個按鈕在同一行中水平放置相等數量的可用空間。 我用android:layout_gravity。問題是什麼?在LinearLayout中放置3個按鈕以佔用相等的空間量

佈局的xml:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="1.0" 
    > 

    <Button android:id="@+id/button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/Bg" 
      android:background="@drawable/button_red" 
      android:layout_weight=".2" 
      android:layout_gravity="left" 
    /> 
    <Button android:id="@+id/button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/Bm" 
      android:background="@drawable/button_red" 
      android:layout_weight=".2" 
      android:textColor="#ffffff" 
      android:layout_gravity="center" 
    /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/button_red" 
     android:layout_weight=".2" 
     android:text="@string/Bf" 
     android:layout_gravity="right" 
    /> 

</LinearLayout> 

,如果有人看到什麼是錯,謝謝。

+2

,並確保你給不同的ID以按鈕.. – Ronnie 2012-07-26 12:34:16

回答

88

下面的佈局應該工作。權重是LinearLayouts ..

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:weightSum="3" 
> 

    <Button android:id="@+id/button1" 
      ... 
      android:layout_weight="1"/> 

    <Button android:id="@+id/button2" 
      ... 
      android:layout_weight="1"/> 

    <Button 
     android:id="@+id/button3" 
     ... 
     android:layout_weight="1"/> 

</LinearLayout> 
+0

偉大的,我想要的 – user1527152 2012-07-26 12:39:15

+0

完美的邏輯!今天學一個新東西 – 2014-08-25 10:04:07

+0

你是我的生活保護伴侶,我花了4個小時,以瞭解如何做到這一點。謝謝! – 2015-11-24 10:11:36

0

將按鈕放在相對佈局中。將按鈕的屬性添加到allignParentleft = true,另一個allignParentcenter = true和allignParentRight = true。

+0

allignParentcenter不存在,只是左,右...... – user1527152 2012-07-26 12:26:49

+0

對不起嘗試此項到中央按鈕的android :layout_centerInParent = 「真」。 – ShineDown 2012-07-26 12:30:26

+0

這將使它們在一行中對齊,但它不會使它們佔用所有可用空間並使每個按鈕的大小相同 – banzai86 2012-07-26 12:51:02

1

看看這個:

<RelativeLayout 
    android:id="@+id/layout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true"/> 
    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true"> 
     <Button 
      android:id="@+id/button2" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@+id/button1"/> 
    </RelativeLayout> 
    <Button 
     android:id="@+id/button3" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true"/> 
</RelativeLayout> 
+0

@DownVoter在downvoting之前,您是否嘗試過這種佈局? – AkashG 2012-07-26 12:32:36

+0

呃..我不是downvoter。但我認爲他是這麼做的,bcoz你提供了一個基於RelativeLayout的想法,但問題是使用線性佈局來解決它。這聽起來不是一個downvote的原因嗎? – 2012-07-26 12:40:05

+0

@AndroSelva使用線性佈局,可以線性設置3個三個按鈕,但是當屏幕大小發生變化時,它不會保持屏幕中間的方式。通過相對佈局,您可以保持所有屏幕分辨率。 – AkashG 2012-07-26 12:42:58

2

鴻溝的重量總和在所有按鈕比例相等。

刪除layout_gravity屬性並添加android:layout_weight = 0.33。

它會工作

+0

這就是我正在尋找,但我們可以看到在右邊的一些像素。我不知道如何解釋,但只有99%的家長。我不確定我是否可以正確解釋 – user1527152 2012-07-26 12:29:59

+1

將總數更改爲3並將按鈕分配到1中。 – moDev 2012-07-26 12:32:07

+1

噢,爲什麼我不這麼認爲之前-_-「你救了我很多謝謝我正在尋找解決方案 – user1527152 2012-07-26 12:37:33

12

除了設置layout_weight你必須設置layout_widthlayout_height0dp的。所以如果你想要例如水平分配按鈕,layout_width應該是0dp和layout_weight.2或任何數字,只要通過你有的按鈕是相等的。

所以你的佈局應該是這樣的

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
> 

<Button android:id="@+id/button" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="@string/Bg" 
     android:background="@drawable/button_red" 
     android:layout_weight="1" 
/> 
<Button android:id="@+id/button" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="@string/Bm" 
     android:background="@drawable/button_red" 
     android:layout_weight="1" 
     android:textColor="#ffffff" 
/> 

<Button 
    android:id="@+id/button" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:background="@drawable/button_red" 
    android:layout_weight="1" 
    android:text="@string/Bf" 
/> 
</LinearLayout> 
-1

你可以把它像這樣: `

  <RelativeLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_centerVertical="true" 
       android:layout_toLeftOf="@+id/imageButtonLikedPost"> 

       <ImageButton 
        android:id="@+id/imageButtonMyPost" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true" 
        android:layout_centerVertical="true" 
        android:background="@drawable/mypost_tab_bg" /> 
      </RelativeLayout> 

      <ImageButton 
       android:id="@+id/imageButtonLikedPost" 
       android:layout_width="40dp" 
       android:layout_height="30dp" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" 
       android:background="@drawable/mylike_tab_bg" /> 

      <RelativeLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentEnd="true" 
       android:layout_alignParentRight="true" 
       android:layout_centerVertical="true" 
       android:layout_toRightOf="@+id/imageButtonLikedPost"> 

       <ImageButton 
        android:id="@+id/imageButtonMyBlog" 
        android:layout_width="30dp" 
        android:layout_height="30dp" 
        android:layout_centerHorizontal="true" 
        android:layout_centerVertical="true" 
        android:background="@drawable/tab4_bg" /> 
      </RelativeLayout> 
     </RelativeLayout>` 
+0

你好,你介意解釋你的答案嗎?當你問LinearLayout的時候,你使用的是RelativeLayout。我沒有測試你的解決方案,但是問題的寬度是相等的,你的其中一個按鈕是40dp,另一個是40dp 30dp。你能解釋一下嗎? – user1527152 2016-10-06 01:44:28

0

請考慮以下佈局片段:

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="3"> 

    <ImageView 
     android:src="@drawable/logo1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_gravity="left|center_vertical" /> 

    <TextView 
     android:id="@+id/toolbar_title" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="Some Title" 
     android:textColor="@android:color/black" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:layout_weight="1" /> 

    <ImageView 
     android:src="@drawable/logo2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_gravity="right|center_vertical" /> 

</LinearLayout> 

兩件事上面要注意。

答:我創建了一個與LinearLayout中的weightSum 3.

B.內。然後,我創建3種元素的1各有layout_weight,這樣我可以有我的子元素均勻地分佈在它們之間的整個空間。

0

我必須讓它務實而不重

float width = CommonUtills.getScreenWidth(activity); 
    int cardWidth = (int) CommonUtills.convertDpToPixel(((width)/3), activity); 

    LinearLayout.LayoutParams params = 
     new LinearLayout.LayoutParams(cardWidth, 
      LinearLayout.LayoutParams.MATCH_PARENT); 

    btnOne.setLayoutParams(params); 
    btnTwo.setLayoutParams(params); 
    btnThree.setLayoutParams(params); 

    public class CommonUtills { 
     public static float getScreenWidth(Context context) { 
      float width = (float) 360.0; 
      DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); 
      width = displayMetrics.widthPixels/displayMetrics.density; 
      return width; 
     } 
    } 



    <LinearLayout 
    android: layout_width = "match_parent" 
    android: layout_height = "50dp"  
    android: orientation = "horizontal" > 


     <Button 
    android: id = "@+id/btnOne" 
    android: layout_width = "wrap_content" 
    android: layout_height = "match_parent" > </Button> 


     <Button 
    android: id = "@+id/btnTwo" 
    android: layout_width = "wrap_content" 
    android: layout_height = "wrap_content" > </Button> 



     <Button 
    android: id = "@+id/btnThree" 
    android: layout_width = "wrap_content" 
    android: layout_height = "wrap_content" > </Button> 
     </LinearLayout> 
+0

你試過你的解決方案嗎?如果'width'不是3的倍數,那麼會有提示。他可以抵消按鈕。此外,剩下的像素將是黑色的,可能會引人注目。 – user1527152 2017-08-27 14:46:18

+0

我曾嘗試過六個按鈕,並且它在橫向和縱向上都很出色。此外,我檢查了很多設備和它的工作正常,如果你把每個按鈕之外的卡片視圖然後它看起來很好 – 2017-08-29 08:16:31

+0

我沒有找到任何像素相關的問題 – 2017-08-29 08:20:46

相關問題