2012-07-26 27 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

相关问题