Android 如何使所有按钮具有相同的高度(并保持其重量)?

Android 如何使所有按钮具有相同的高度(并保持其重量)?,android,android-layout,button,height,Android,Android Layout,Button,Height,使用android:layout\u height=“match\u parent”只需将按钮拉伸到整个屏幕高度即可。我希望按钮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\u height=“match\u parent”只需将按钮拉伸到整个屏幕高度即可。我希望按钮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="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>

需要更改LinearLayout高度以包裹内容,然后更改所有高度以匹配父项

<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>

这样就可以了:

<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>

使父布局高度<代码>包裹内容与第二个按钮的高度<代码>匹配\u父布局


你已经做对了。第二个按钮的宽度已经是其余按钮的两倍


剩下的问题是高度。高度上的差异是因为按钮内的文本对于这样小的按钮来说太大了。只需减小文本大小,就可以发挥神奇的作用。

您更改了按钮2的重量。我希望能够玩宽度权重。减少文本大小android:textSize=“10dp”。减小所有文本的文本大小buttons@Raghunandan这不是一个可接受的解决方案。我想保持一切原样,文本大小、颜色、重量、按钮顺序,只需按钮2,在这种情况下,我希望它与同一行上其他按钮的高度相匹配,但这是不可能的。我很好奇,期待着一个更好的solution@Raghunandan请参阅已接受的答案+我的答案comment@Raghunandan这就是这个问题的全部要点,我不希望文本在同一行。我希望按钮的高度是相同的,无论它们显示多少行文本。刚刚测试过,它会产生相同的结果。抱歉,但你似乎不知道你在说什么。如果你读了这个问题,你会发现我确实尝试了
匹配父母
,但它不起作用。如果你在发布之前尝试一下你的建议,那就太好了,除非你完全确定解决方案,并且可以用一些文字解释为什么它能解决它所解决的问题。发布之前,我在一台设备上尝试了更新后的解决方案。如果您将LinearLayout的布局高度更改为wrap内容,则匹配父项将起作用。现在您是正确的,但您忘了在代码中更改它。这是公认的解决方案,由Archie.bpgc甚至填充垂直空间,并按照@Archie.bpgc的建议使第二个按钮“匹配父项”,这几乎就是解决方案。原因是我在这里举了一个按钮文本“BTNx”的例子。在现实生活中,按钮的文本是事先不知道的。我尝试了您的解决方案,将父线性布局高度设置为
wrap\u content
,但所有按钮(而不仅仅是“BTN2”)高度都设置为
match\u parent
,效果很好,这是我的完整解决方案。谢谢。如果按钮的文本是三行的,这不起作用。为什么?
<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>