2013-05-13 87 views
2

enter image description here如何让所有按钮的高度相同(并保持它们的重量)?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:weightSum="6" 
    tools:context=".MainActivity" > 

    <Button 
     android:text="BTN1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill_vertical" 
     android:layout_weight="1" /> 
    <Button 
     android:text="BTN2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill_vertical" 
     android:layout_weight="2" /> 
    <Button 
     android:text="BTN3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill_vertical" 
     android:layout_weight="1" /> 
    <Button 
     android:text="BTN4" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill_vertical" 
     android:layout_weight="1" /> 
    <Button 
     android:text="BTN5" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill_vertical" 
     android:layout_weight="1" /> 

</LinearLayout> 

使用android:layout_height="match_parent"只是拉伸按钮将整个屏幕的高度。我希望按钮2的宽度是其他按钮的两倍。

+0

缩小文字大小android:textSize =“10dp”。减少所有按钮的文字大小 – Raghunandan 2013-05-13 18:18:34

+1

@Raghunandan这不是一个可接受的解决方案。我想保留所有内容,文本大小,颜色,重量,按钮顺序,按钮2,在这种情况下,我希望它匹配同一行上其他按钮的高度 – ilomambo 2013-05-13 18:20:38

+0

然后它不会认为它是可能的。以及我很好奇,并期待更好的解决方案 – Raghunandan 2013-05-13 18:22:10

回答

2

这样做:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="6" 
    tools:context=".MainActivity" > 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill_vertical" 
     android:layout_weight="1" 
     android:text="BTN1" /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="fill_vertical" 
     android:layout_weight="2" 
     android:text="BTN2" /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill_vertical" 
     android:layout_weight="1" 
     android:text="BTN3" /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill_vertical" 
     android:layout_weight="1" 
     android:text="BTN4" /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill_vertical" 
     android:layout_weight="1" 
     android:text="BTN5" /> 

</LinearLayout> 

使父布局高度wrap_content和第二按钮的高度match_parent

enter image description here

+2

这是几乎解决方案。原因是我在这里举了一个按钮文本“BTNx”的例子。在现实生活中,按钮的文本未预先知道。我尝试了将父LinearLayout高度设置为'wrap_content'的解决方案,但所有按钮(而不是“BTN2”)高度设置为'match_parent',并且工作正常,这对我来说是完整的解决方案。谢谢。 – ilomambo 2013-05-13 18:56:55

+0

如果按钮的文字是三行,这不起作用。为什么? – anshul 2015-04-13 11:39:37

0

在这里你去...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:weightSum="6" 
    tools:context=".MainActivity" > 

    <Button 
     android:id="@+id/Button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_gravity="fill_vertical" 
     android:layout_toLeftOf="@+id/Button2" 
     android:layout_weight="1" 
     android:text="BTN1as s dh asgdjhsg djahgsd gsadhasdhg sa" /> 

    <Button 
     android:id="@+id/Button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/Button1" 
     android:layout_alignParentTop="true" 
     android:layout_gravity="fill_vertical" 
     android:layout_toLeftOf="@+id/Button3" 
     android:layout_weight="1" 
     android:text="BTN2" /> 

    <Button 
     android:id="@+id/Button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/Button1" 
     android:layout_alignParentTop="true" 
     android:layout_gravity="fill_vertical" 
     android:layout_toLeftOf="@+id/Button4" 
     android:layout_weight="1" 
     android:text="BTN3" /> 

    <Button 
     android:id="@+id/Button4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/Button1" 
     android:layout_alignParentTop="true" 
     android:layout_gravity="fill_vertical" 
     android:layout_toLeftOf="@+id/Button5" 
     android:layout_weight="1" 
     android:text="BTN4" /> 

    <Button 
     android:id="@+id/Button5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/Button1" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_gravity="fill_vertical" 
     android:layout_weight="1" 
     android:text="BTN5" /> 

</RelativeLayout> 
+0

你改变了按钮2的重量。我希望能够玩宽度权重。 – ilomambo 2013-05-13 18:15:11

-1

需要改变的LinearLayout高度WRAP_CONTENT,然后更改所有以match_parent的高度。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="6" 
    tools:context=".MainActivity" > 

    <Button 
     android:id="@+id/button1" 
     android:text="BTN1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="fill_vertical" 
     android:layout_weight="1" /> 
    <Button 
     android:id="@+id/button2" 
     android:text="BTN2" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="fill_vertical" 
     android:layout_weight="2" /> 
    <Button 
     android:id="@+id/button3" 
     android:text="BTN3" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="fill_vertical" 
     android:layout_weight="1" /> 
    <Button 
     android:id="@+id/button4" 
     android:text="BTN4" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="fill_vertical" 
     android:layout_weight="1" /> 
    <Button 
     android:id="@+id/button5" 
     android:text="BTN5" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="fill_vertical" 
     android:layout_weight="1" /> 

</LinearLayout> 
+0

刚刚测试过它,它会产生相同的结果。 – ilomambo 2013-05-13 18:19:00

+0

刚刚更新了代码。 – 2013-05-13 18:23:04

+0

对不起,但你似乎不知道你在说什么。如果你阅读了这个问题,你会发现我尝试了'match_parent',但它不起作用。如果你在发布之前尝试你的建议,会很好,除非你完全确信解决方案,并且还可以解释为什么解决它所解决的问题。 – ilomambo 2013-05-13 18:48:36

0

你已经这样做是正确的。第二个按钮已经是其余按钮宽度的两倍。

您留下的问题是高度。高度差异是因为按钮内的文字对于这样的小按钮来说太大了。只是减少文本大小,这应该做的魔术。

+0

即使填充垂直空间,并按照@ Archie.bpgc的建议让第二个按钮“匹配父对象” – 2013-05-13 18:34:46

相关问题