【问题标题】:How can I limit the width of a button within a TableRow in Android?如何在 Android 的 TableRow 中限制按钮的宽度?
【发布时间】:2014-05-16 21:48:01
【问题描述】:

我希望按钮的宽度尽可能小。我在布局文件中的 XML 是:

<TableRow
    android:id="@+id/tableRow7"
    android:paddingLeft="5dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:text="@string/button_ok"
        android:layout_column="0"
        android:id="@+id/buttonOK" />

    <Button
        android:layout_width="20dp"
        android:maxWidth="20dp"
        android:layout_height="wrap_content"
        android:text="@string/button_cancel"
        android:layout_column="1"
        android:id="@+id/buttonCancel" />
</TableRow>

我已经为 layout_width 值尝试了“wrap_content”和“fill_parent”,但它们没有帮助。如您所见,我也尝试了“maxWidth”属性,但无济于事。这是它的样子:

那么如何将按钮的宽度限制为“刚好足够”以包含其文本?我希望“取消”按钮的宽度与“确定”按钮的宽度大致相同,或者尽可能多一些以包含其文本。

【问题讨论】:

    标签: android xml android-layout android-button android-tablelayout


    【解决方案1】:

    那么如何将按钮的宽度限制为“刚好足够”以包含其文本?

    理想情况下,从表格中删除它(和确定按钮)。

    TableLayout 背后的意义在于为您提供类似于 HTML 表格的特征,尤其是对于像我这样在 1990 年代 CSS 之前的 Web 开发中过度使用 HTML 表格的白胡子人士。

    在 HTML 表格中,单元格会展开以填充列中最宽的单元格的宽度。这里也一样——取消按钮的宽度由它的列决定,我认为它位于由条形图的条形控制的列中。

    从屏幕截图中,TableLayout 中唯一应该是四行条形图本身。 “编辑列表”标签 (?)、单选按钮和确定/取消按钮应位于 TableLayout 下方。如果您希望“确定”和“取消”按钮的宽度相同,请为它们使用水平的LinearLayout,为它们各自指定宽度0dp,并为它们各自指定权重1

    【讨论】:

    • 这不是条形图,而是一组 EditText 小部件。不过,我会试试你的方法。
    • @B.ClayShannon:考虑到它们与用户习惯看到的EditText 小部件不同,您认为用户会认出它们是EditText 小部件吗?
    • 他们只是有不同的背景(EditTexts,而不是用户 - android:background="#ffcb05")。与这种颜色的独特性相比,我们的用户习惯看到的就像患有象皮病的鸭嘴兽。
    • 实际上,我希望按钮尽可能窄,而不是简单地保持相同的宽度;你的方法确实有效,但我的方法更适合这种情况(对我来说,无论如何)。
    • @B.ClayShannon:然后使用wrap_content作为宽度并去掉LinearLayout内的权重。
    【解决方案2】:

    我知道了 - 边距属性可以是你的朋友。我将其添加到“取消”按钮中,该按钮已简化为:

    <Button
        android:layout_marginRight="9![enter image description here][1]0dp"
        android:text="@string/button_cancel"
        android:layout_column="1"
        android:id="@+id/buttonCancel" />
    

    ...现在我更喜欢它了:

    更新

    Common swear 有一个好主意:

    <LinearLayout
        android:id="@+id/tableRow7"
        android:paddingLeft="112dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_ok"
            android:id="@+id/buttonOK" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_cancel"
            android:id="@+id/buttonCancel" />
    </LinearLayout>
    

    【讨论】:

    • 这太可怕了,当您尝试其他屏幕尺寸时会非常严重地失败。
    • 这是一个单一设备的应用程序。它只能在 TC55 上运行。
    • 但是,Stack Overflow 并不是您独有的资源。对于将来访问此问题的其他人来说,了解这种方法的局限性非常重要。
    • 好点;我认为“你”是指我,但我猜你是指你。
    • 对不起,我应该更具体一些。或者不太具体。任何。 :-) 虽然像您这样的单设备开发人员很少,但绝大多数 Android 开发人员都针对多个设备。这不可避免地意味着屏幕尺寸和密度各不相同。处理一堆维度值来处理这些变化是一件痛苦的事情,最好尽可能避免。
    猜你喜欢
    • 2011-09-09
    • 1970-01-01
    • 2018-08-03
    • 2011-09-19
    • 2019-12-12
    • 2011-10-26
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    相关资源
    最近更新 更多