Android TextView 常見問題與使用總結,androidtextview
一、文字顯示行數設定
1. 僅顯示一行文字android:singleLine="true"
setTransformationMethod(TransformationMethod)
2. 設定最多顯示幾行文字
android:maxLines="3"
TextView.setMaxLines(3);
3. 設定文字最大顯示行數後,但是文字可能顯示不全,可以設定省略符號,表示還有內容還未顯示android:ellipsize="end" end - 省略符號在結尾,最常用方式start - 省略符號在開頭 middle - 省略符號在中間 marquee - 跑馬燈顯示,總是執行文字滾動動畫,影響效能
4. 不顯示padding
android:includeFontPadding="false"
5. 行間距
android:lineSpacingExtra=""
二、文字顯示長度設定1. 設定最大顯示長度,單位是pxandroid:maxWidth="" / TextView.setMaxWidth(int)
左中右三個TextView 使用 RelativeLayout 布局, 中間的文字如果會改變長度的話,不會自動調整,導致右側文字還是靠右
使用maxWidth可以解決以上問題
2. 最多顯示字元個數android:maxLength="" / TextView.setFilters(InputFilter)不會區分中文還是英文,例如設定3,可以顯示3個漢字,但是也僅能顯示3個英文單字。
3. 動態修改文字大小需要注意,不能直接使用TextView.setSize(pxValue);setTextSize (TypedValue.COMPLEX_UNIT_PX, pxValue);
文字大小類型詳見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));
$d 是預留位置,更多資訊詳見文檔Formatterhttp://developer.android.com/reference/java/util/Formatter.html
2. 一行文字多種顏色<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));
3. 點擊文字改變顏色textView.setTextColor( getResources().getColorStateList(R.color.selector) );
四、 android drawTextTextView注意
忽略了baseLine 到 Bottom的距離http://blog.csdn.net/liucheng2009/article/details/7053837