Android TextView 常見問題與使用總結,androidtextview

來源:互聯網
上載者:User

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">&lt;font color=\"#aaaaaa\"&gt;%1$s文字部分&lt;/font&gt;</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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.