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

ilo*_*mbo 3 height android button android-layout

在此处输入图片说明

<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>
Run Code Online (Sandbox Code Playgroud)

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

Arc*_*pgc 5

这样可以:

<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>
Run Code Online (Sandbox Code Playgroud)

设置父布局的高度wrap_content和第二个按钮的高度match_parent

在此处输入图片说明

  • 那几乎是解决方案。原因是在这里我举了一个带有按钮文本“ BTNx”的示例。在现实生活中,按钮的文本是未知的。我尝试了将父级LinearLayout高度设置为`wrap_content`的解决方案,但所有按钮(而不仅仅是“ BTN2”)的高度都设置为`match_parent`,并且效果很好,这对我来说是完整的解决方案。谢谢。 (2认同)