2011年10月24日月曜日

TextViewの文字列を一行に収め、改行しないようにする【android】【TextView】【Layout】

TextViewの内容をユーザが入力する場合、想定されたサイズを超過して、
2行に渡り表示されてしまうようなことが、しばしばあります。

このようなときに、長い文字列の先頭や中間、最後について「…」に
置き換えて表示するオプションがTextViewに存在しています。

android:scrollHorizontally="true"
android:ellipsize="start"

scrollHorizontally については固定で"true"を設定し、ellipsize
について、先頭を省略したい場合は"start",中間を省略したい場合は"middle"、
最後を省略したい場合は、"end"を指定します。


たとえば、下記のように設定すると、画面の操作で入力されたテキストについて、
先頭が「…」で省略され、表示されます。

    <TextView android:id="@+id/textview11"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_gravity="left"
android:layout_marginLeft="2dip"
android:layout_marginTop="2dip"
android:textColor="@color/white"
android:textSize="35dip"
android:textStyle="bold"
android:scrollHorizontally="true"
android:ellipsize="start"
/>

0 件のコメント:

コメントを投稿