【问题标题】:How can I force my checkbox's text to not wrap?如何强制我的复选框的文本不换行?
【发布时间】:2014-06-04 22:24:12
【问题描述】:

这就是我的 LinearLayout(水平)行的样子:

我希望复选框的文本位于一行;按钮不必那么宽 - 它们仍然有足够的空间,复选框文本加长了一点。我的 XML 中有什么:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/ckbxAllow_New_Items"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:checked="true"
        android:text="@string/checkbox_Allow_New_Items" />

    <Button
        android:id="@+id/btnOK"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_OK" />

    <Button
        android:id="@+id/btnCancel"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/button_Cancel" />

</LinearLayout>

...需要更改以强制我的复选框文本不换行吗?

更新

按照 Der Golem 的建议,添加以下内容:

android:lines="1"

...并且还将复选框的 layout_weight 从 1 更改为 2(按钮设置为 1)给了我我想要的:

【问题讨论】:

    标签: android-layout android-linearlayout android-button android-checkbox


    【解决方案1】:

    CheckBox 继承自 CompoundButton,它继承自 Button,而 Button 继承自 TextView。所以,它拥有这些祖先的所有属性、方法和属性...
    参考:http://developer.android.com/reference/android/widget/CheckBox.html

    特别是,您对 TextView 属性、方法和属性感兴趣:
    参考:http://developer.android.com/reference/android/widget/TextView.html

    特别是,您对android:lines 属性感兴趣,并将其设置为1。
    这告诉您的 CheckBox 正好高 1 行。

    您可能还想将android:ellipsize 属性设置为某个值(即:3 = 结束)。
    这告诉您的 CheckBox 将三个点(省略号)添加到截断文本的结尾、开始、中心...。

    [编辑]

    作为TextView的出身,它可以使用setSingleLine - 感谢@CrandellWS的评论。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多