【问题标题】:Buttons in LinearLayout issueLinearLayout 问题中的按钮
【发布时间】:2012-04-10 07:46:11
【问题描述】:

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

【问题讨论】:

  • 我认为 Jason 是在问:请为您的布局发布 XML,以便我们查看并有意义地回答
  • 我认为这个问题不值得被否决。这是一个非常有效和明确的问题。是的,在查明错误时可能需要 XML,但我认为即使没有当前的布局实现,也可以给出几个好的答案。各位,请不要对你的反对票感到太高兴!

标签: android button dialog android-linearlayout


【解决方案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>

【讨论】:

  • 还有;如果您希望所有按钮具有相同的宽度,则应将所有按钮的 android:layout_width 属性设置为“0dp”。在@Agarwal 给出的示例中,您的按钮将首先在包裹内容时获得一个宽度(“联系人”按钮将比“添加”按钮更宽),然后剩余的宽度均匀分布在三个按钮之间。如果它们都以零宽度开始,则在最终大小重新分配后它们将具有完全相同的宽度。我真的以为我看到了解释这一点的答案,但显然我的眼睛在欺骗我......
【解决方案2】:

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

  1. 在父LinearLayout中设置android:weightSum="2"
  2. Button 中设置android:layout_weight="1"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-14
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多