How can I force my checkbox's text to not wrap?

别来无恙 提交于 2019-12-10 21:09:04

问题


This is what my LinearLayout (horizontal) row looks like:

I want the text of the checkbox to be on one line; The buttons don't have to be that wide - they'll still have plenty of space with the checkbox text lengthened a bit. What in my 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>

...needs to change in order to force my checkbox text not to wrap?

UPDATE

Following Der Golem's suggestion by adding this:

android:lines="1"

...and also changing layout_weight for the checkbox from 1 to 2 (set to 1 for the buttons) gave me what I wanted:


回答1:


CheckBox inherits from CompoundButton, which inherits from Button, which inherits from TextView. So, it has all the properties, methods and attributes of these ancestors...
Reference: http://developer.android.com/reference/android/widget/CheckBox.html

In particular, you are interested to the TextView properties, methods and attributes:
Reference: http://developer.android.com/reference/android/widget/TextView.html

In particular, you're interested in the android:lines attribute, and set it to 1.
This tells your CheckBox to be exactly 1 line tall.

You might also want to set the android:ellipsize attribute to some value (i.e.: 3 = end).
This tells your CheckBox to add three dots (ellipsis) to the end, start, center, ... of the truncated text.

[EDIT]

Being a decentant of TextView, it can use setSingleLine - Thanks to @CrandellWS for the comment.



来源:https://stackoverflow.com/questions/24048618/how-can-i-force-my-checkboxs-text-to-not-wrap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!