简体   繁体   中英

android:textview adjusts itself to wrong layout_width

<TextView 
    android:layout_height="wrap_content" 
    android:id="@+id/LineNumberTextView"
    android:hint="Number" 
    android:textSize="15sp" 
    android:layout_width="wrap_content"
    android:paddingRight="10sp">
</TextView>

The above code creates a wrong view: when the text is replaced (in code) to a short (1-3 digits) number, the view width stays about 6 characters - resulting in way too much padding. This problem occurs in some other places in the application too. One option is to set the hint to 1-3 characters - but this seems ugly, and doesn't solve the problem for too-long text (which also suffers from hint width problems). Therefore, I'm looking for a more elegant solution (which does not require recalculating width on each change), if there is one.

BTW, the input text is set exactly once for each occurrence of this layout, when loading it into some list view (but it's dynamic and unknown when loading the application). Problem is most severe in android 2.1, but appears in some other platforms too.

set "minems" as per your requirement, so that your textview will streached to a proper width. set a "maxems" so that it will limited to a certain width.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM