【问题标题】:Android: how to set two buttons in vertical linear layout to same width?Android:如何将垂直线性布局中的两个按钮设置为相同的宽度?
【发布时间】:2012-07-31 05:57:37
【问题描述】:

在我的 Android 应用程序中,我想创建一个对话框窗口,其中包含顶部的图像、中间的一些信息文本和下面的两个按钮。这两个按钮位于垂直方向的线性布局中。两者的宽度应该相同。

我已经设法创建了一个与描述类似的布局,但是,带有较长文本的按钮变得比另一个更宽。在附图中,下方的按钮比上方的按钮宽一点,如红色虚线所示。

我用于这种内部线性布局的布局如下:

<LinearLayout
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <Button 
        android:id="@+id/close_dialog_button_ok" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="@string/upload_dialog_ok"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:layout_marginTop="10dip" />        

    <Button 
        android:id="@+id/close_dialog_button_cancel" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="@string/upload_dialog_cancel"
        android:layout_marginRight="10dip"
        android:layout_marginTop="10dip"
        android:layout_marginBottom="5dip" />        

</LinearLayout>

有什么想法我在这里做错了吗?
提前感谢您的帮助!

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    你忘了在第二个按钮上设置android:layout_marginLeft="10dip"

    【讨论】:

    • 我以为他知道这一点。如果他没有在他的实际项目中设置,那么这将是正确的答案。如果没有,请检查我的。
    • 天哪!真丢脸...感谢您的快速回复,您当然是对的 :)
    【解决方案2】:

    如果您希望它们相同,则必须将 'android:layout_width="fill_parent"' 更改为 '200dp' 之类的东西。由于里面的文字更长,该应用程序会使其中一个更长。所以尝试将两个按钮都设置为:

     android:layout_width="200dp"
    

    它们将是相同的,并且由于使用了“dps”,它们在所有屏幕尺寸上仍应保持正确的比例。

    【讨论】:

    • 我认为您将wrap_contentmatch_parent(又名fill_parent)混淆了。无论按钮的内容如何,​​后者都将匹配其父级的内部宽度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 2015-10-23
    • 2016-05-18
    • 1970-01-01
    • 2014-05-18
    相关资源
    最近更新 更多