TextView
TextView直接繼承了View,從功能上看TextView就是一個沒有編輯功能的文本編輯器,主要用於文本內容的展示。TextView有非常多的XML屬性,主要使用的有以下幾種:
(1)顯示文字
(2)給文字添加圖標
(3)給文字添加添加下劃線
(4)給文字添加中劃線
(5)文本框的顯示不下內容時的操作
顯示文字
(1)text
給View設置文本信息。有兩種方法可以給text添加文字:
(1)android:text=“HelloWorld” 直接給text賦值
(2)android:text="@string/text" 將文本內容寫在string.xml中,然後賦值
(2)textSize
設置TextView中文本的字體大小,字體的單位建議使用sp
(3)textColor
設置TextView的文本顏色
(4)layout_margin
設置外邊距,即TextView和其他組件的間隔,注意直接使用layout_margin是爲該組件的上下左右都設置外邊距,也可以分別給組件的四個方向設置外邊距。
(5)padding
設置內邊距,即TextView中顯示的內容了TextView邊框的距離,和layout)margin一樣,可以通過padding一起設置,也可以分別設置。
示例程序
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="10dp"
android:padding="10dp"
android:padding="10dp"
android:text="@string/textPHP"
android:textColor="#ff0000"
android:textSize="24sp" />
給文字添加圖標
(6)drawableLeft
將圖標設置在文字的左側。除此之外android還提供了drawableRight等,可以將圖標放在文本的上下左右。
示例程序
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="15dp"
android:drawableLeft="@drawable/setting"
android:drawablePadding="5dp"
android:text="設置"
android:textColor="#0000ff"
android:textSize="24sp" />
給文字添加添加下劃線
給文字添加下劃線需要使用代碼才能實現
(7)setUnderlineText(boolean)
該方法是TextPaint中的一個方法,通過該方法可以爲TextView中的內容設置下劃線,不過使用該函數之前必須獲得畫該組件的畫筆。
(8)getPaint()
獲取畫筆
示例程序
m_TextViewJAVA=findViewById(R.id.textViewJAVA);
//獲取畫筆
TextPaint textPaint=m_TextViewJAVA.getPaint();
//給文本設置下劃線
textPaint.setUnderlineText(true);
給文本添加中劃線
(9)setFlags()
給文本這隻標誌,該函數並不僅僅只是單純的智能設置中劃線,還可以設置其他很多樣式,如下劃線等。
示例程序
m_TextViewC.getPaint().setFlags(TextPaint.STRIKE_THRU_TEXT_FLAG);
文本框的顯示不下內容時的操作
(10)ellipsize
設置當顯示的文本超過了TextView的長度是如何處理文本內容,該屬性支持如下屬性值:
(1)none:不做任何處理
(2)start:在文本開始出截斷,並顯示省略號
(3)middle:在文本中間截斷,並顯示省略號
(4)end:在文本末尾處階段,並顯示省略號
(5)marquee:使用marquee滾動動畫顯示文本
(11)maxLines
設置組件內容的最大行數
(12)singleline
設置文本內容文單行顯示
(13)focusable
設置焦點
(14)focusableInToucnMode
設置可以通過觸摸獲取焦點
(15)marqueeRepeatLimit
設置文本內容的滾動次數,marquee_forever爲無限次。
示例程序1
將顯示不下的內容從文本末尾截斷,並使用省略號
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="10dp"
android:ellipsize="end"
android:maxLines="1"
android:text="@string/textCPP"
android:textColor="#03A9F4"
android:textSize="24sp" />
示例程序2
將顯示不下的內容以滾動形式顯示在屏幕上
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="15dp"
android:text="@string/textPoetry"
android:textColor="#43F436"
android:textSize="24sp"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:focusable="true"
android:focusableInTouchMode="true" />
運行結果
以上幾種效果的運行結果:
String.xml
<resources>
<string name="app_name">ViewText</string>
<string name="textPHP">PHP是世界上最好的語言</string>
<string name="textCPP">CPP是世界上最好的語言</string>
<string name="textC">C語言是世界上最好的語言</string>
<string name="textJAVA">JAVA是世界上最好的語言</string>
<string name="textPoetry">待到秋來九月八,我花開後百花殺。沖天香陣透長安,滿城盡帶黃金甲。</string>
</resources>