Android 线性布局问题中的按钮

Android 线性布局问题中的按钮,android,button,dialog,android-linearlayout,Android,Button,Dialog,Android Linearlayout,我有一个自定义对话框,它有3个按钮,有时只有一个按钮。当我有3个按钮时,LinearLayout本身非常适合这些按钮。但是,当我只有一个按钮时,它会将整个宽度提供给一个按钮,使该按钮看起来太大。我想要的是,如果只有一个按钮,它应该只占整个可用宽度的一半,或者应该包装内容(按钮图像)。请参阅以下图像以供参考- 要使一个按钮占据可用屏幕宽度的一半,您需要 在父级LinearLayout 在按钮中设置android:layout_weight=“1” 为了让一个按钮占据可用屏幕宽度的一半,您需要 在

我有一个自定义对话框,它有3个按钮,有时只有一个按钮。当我有3个按钮时,
LinearLayout
本身非常适合这些按钮。但是,当我只有一个按钮时,它会将整个宽度提供给一个按钮,使该按钮看起来太大。我想要的是,如果只有一个按钮,它应该只占整个可用宽度的一半,或者应该包装内容(按钮图像)。请参阅以下图像以供参考-


要使一个按钮占据可用屏幕宽度的一半,您需要

  • 在父级
    LinearLayout
  • 按钮中设置
    android:layout_weight=“1”

  • 为了让一个按钮占据可用屏幕宽度的一半,您需要

  • 在父级
    LinearLayout
  • 按钮中设置
    android:layout_weight=“1”

  • 请参考与您的要求类似的XML文件。只是“转到非必需”按钮的可见性

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" android:weightSum="3" android:gravity="center_horizontal">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button"/>
    
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />
    
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button"/>
    
    </LinearLayout>
    

    请参考与您的要求类似的XML文件。只是“转到非必需”按钮的可见性

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" android:weightSum="3" android:gravity="center_horizontal">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button"/>
    
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />
    
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button"/>
    
    </LinearLayout>
    
    
    
    我想Jason是在问:请为您的布局发布XML,这样我们可以看一看并有意义地回答。我认为这个问题不值得投反对票。这是一个非常有效和明确的问题。是的,在确定错误时可能需要XML,但我认为即使没有当前的布局实现,也可以给出几个很好的答案。各位,请不要对你们的反对票感到太兴奋!我想Jason是在问:请为你的布局发布XML,这样我们就可以看一看并有意义地回答。我不认为这个问题值得投反对票。这是一个非常有效和明确的问题。是的,在确定错误时可能需要XML,但我认为即使没有当前的布局实现,也可以给出几个很好的答案。各位,请不要对你们的反对票感到太兴奋!而且如果希望所有按钮具有相同的宽度,则应将所有按钮上的
    android:layout\u width
    属性设置为“0dp”。根据@Agarwal给出的示例,您的按钮在包装内容时首先会得到一个宽度(“联系人”按钮会比“添加”按钮宽),然后剩余的宽度会均匀分布在三个按钮之间。如果它们都以零宽度开始,则在最终尺寸重新分配后,它们都将具有完全相同的宽度。我真的以为我看到了一个解释这一点的答案,但显然我的眼睛在欺骗我……而且;如果希望所有按钮具有相同的宽度,则应将所有按钮上的
    android:layout\u width
    属性设置为“0dp”。根据@Agarwal给出的示例,您的按钮在包装内容时首先会得到一个宽度(“联系人”按钮会比“添加”按钮宽),然后剩余的宽度会均匀分布在三个按钮之间。如果它们都以零宽度开始,则在最终尺寸重新分配后,它们都将具有完全相同的宽度。我真的以为我看到了一个解释这个的答案,但显然我的眼睛在欺骗我。。。