一、文字顯示行數設置
1. 僅顯示一行文字
android:singleLine="true"
setTransformationMethod(TransformationMethod)
setTransformationMethod(TransformationMethod)
2. 設置最多顯示幾行文字
android:maxLines="3"
TextView.setMaxLines(3);
android:maxLines="3"
TextView.setMaxLines(3);
3. 設置文字最大顯示行數后,可是文字可能顯示不全。能夠設置省略符號。表示還有內容還未顯示
android:ellipsize="end"
end - 省略號在結尾。最經常使用方式
start - 省略號在開頭
middle - 省略號在中間
marquee - 跑馬燈顯示,總是運行文字滾動動畫。影響性能
4. 不顯示padding
android:includeFontPadding="false"
android:includeFontPadding="false"
5. 行間距
android:lineSpacingExtra=""
android:lineSpacingExtra=""
二、文字顯示長度設置
1. 設置最大顯示長度。單位是px
android:maxWidth="" / TextView.setMaxWidth(int)
左中右三個TextView 使用 RelativeLayout 布局, 中間的文字假設會改變長度的話,不會自己主動調整,導致右側文字還是靠右
使用maxWidth能夠解決以上問題
2. 最多顯示字符個數
左中右三個TextView 使用 RelativeLayout 布局, 中間的文字假設會改變長度的話,不會自己主動調整,導致右側文字還是靠右
使用maxWidth能夠解決以上問題
2. 最多顯示字符個數
android:maxLength="" / TextView.setFilters(InputFilter)
不會區分中文還是英文。比如設置3,能夠顯示3個漢字,可是也僅能顯示3個英文單字。
3. 動態改動文字大小
須要注意。不能直接使用TextView.setSize(pxValue);
setTextSize (TypedValue.COMPLEX_UNIT_PX, pxValue);
文字大小類型詳見TypedValue文檔
文字大小類型詳見TypedValue文檔
三、TextView 設置不相同式文本
1. 文字占位符
有些時候一段字符大部分都是固定的。僅中間一兩位是變動的,並且想在string.xml中進行聲明文本部分。比如:“2015年03月14日”
在res/values/string.xml中
<resources>
<string name="date">2015年03月%1$d日</string>
</resources>
textView.setText(getResoure.getString(R.string.date, 14));
有些時候一段字符大部分都是固定的。僅中間一兩位是變動的,並且想在string.xml中進行聲明文本部分。比如:“2015年03月14日”
在res/values/string.xml中
<resources>
<string name="date">2015年03月%1$d日</string>
</resources>
textView.setText(getResoure.getString(R.string.date, 14));
$d 是占位符,很多其它信息詳見文檔
Formatter
http://developer.android.com/reference/java/util/Formatter.html
2. 一行文字多種顏色
<resources>
<string name="love_world"><font color=\"#aaaaaa\">%1$s文字部分</font></string>
</resources>
<string name="love_world"><font color=\"#aaaaaa\">%1$s文字部分</font></string>
</resources>
使用時須要
String string = getResources().getString(R.string.
love_world, nameStr);
TextView.setText( Html.fromHtml(string));
TextView.setText( Html.fromHtml(string));
3. 點擊文字改變顏色
textView.setTextColor( getResources().getColorStateList(R.color.selector) );
四、 android drawTextTextView注意
忽略了baseLine 到 Bottom的距離
五、設置文字使用什么單位
文字大小使用dip。由於即使屏幕密度同樣的不同手機sp轉換為px的值不一致