簡介
TextView是Android中最常見的控件之一,常用來顯示一段文本。TextView繼承于View,我們經(jīng)常在xml布局文件中對TextView屬性進(jìn)行設(shè)置,本文我們將對TextView的常用屬性進(jìn)行講解。如果想了解更多,也可以查閱官方文檔,TextView
常用屬性
- id :設(shè)置控件的id 用于java代碼中找到該控件
- layout_width : 控件的寬度 常用值 match_parent(填充父容器) wrap_content(包裹內(nèi)容)
- layout_height : 控件的高度 常用值 match_parent(填充父容器) wrap_content(包裹內(nèi)容)
- text : 文本內(nèi)容(建議使用@string/xxx的形式設(shè)置)
- background : 背景填充(使用背景顏色或drawable資源)
- textColor textSize textStyle: 字體顏色 字體大小 字體樣式
- gravity : 字體或內(nèi)容的對齊方式
- layout_gravity :控件的對齊方式
效果預(yù)覽
現(xiàn)在我們使用相關(guān)屬性實(shí)現(xiàn)一些TextView的效果,預(yù)覽圖如下:

textview.gif
基礎(chǔ)樣式
我們在布局文件中添加一個(gè)TextView控件
<TextView
android:id="@+id/tv_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingHorizontal="16dp"
android:text="示例文本 example"
android:layout_gravity="center_horizontal"
android:textColor="#00FA9A"
android:textSize="16sp"
android:textStyle="italic|bold"/>
- 設(shè)置字體顏色中綠色:
textColor="#00FA9A" - 設(shè)置字體大小16sp:
textSize="16sp" - 設(shè)置字體形狀斜體加粗:
textStyle="italic|bold"
背景邊框
<TextView
android:id="@+id/tv_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingHorizontal="16dp"
android:text="示例文本 example"
android:layout_gravity="center_horizontal"
android:textColor="#DC143C"
android:textSize="16dp"
android:background="@drawable/textview_background"/>
- 設(shè)置控件背景:
background="@drawable/textview_background"
background屬性可以為控件添加背景,其值既可以是顏色值,也可以是drawable資源。上面我們使用了drawable,并設(shè)置了邊框,drawable代碼如下
textview_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 實(shí)心 -->
<solid android:color="#FFFFFF"/>
<!-- 邊框 -->
<stroke
android:width="0.5dp"
android:color="#FFA500"/>
<!-- 圓角 -->
<corners android:radius="3dp"/>
<!-- 邊距 -->
<padding
android:top="2dp"
android:bottom="2dp"
android:left="6dp"
android:right="6dp"/>
</shape>
背景漸變
我們可以通過修改上面的drawable實(shí)現(xiàn)漸變色背景
textview_gradient_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#FFA500"
android:endColor="#FF00FF"
android:angle="360"
android:centerX="0.5"
android:centerY="0.5"/>
<corners
android:radius="3dp"/>
</shape>
- 設(shè)置背景:
background="@drawable/textview_gradient_background"
字體漸變
以下代碼實(shí)現(xiàn)文本漸變
<TextView
android:id="@+id/tv_text_gradient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingHorizontal="16sp"
android:text="示例文本 example"
android:layout_gravity="center_horizontal"
android:textSize="16dp" />
Java
TextView textGradient = (TextView)findViewById(R.id.tv_text_gradient);//找到控件
LinearGradient mLinearGradient = new LinearGradient(0, 0,
tv_text_gradient.getPaint().getTextSize()* tv_text_gradient.getText().length(),0,
Color.parseColor("#FFFF68FF"),
Color.parseColor("#FFFED732"),
Shader.TileMode.CLAMP);//創(chuàng)建線性漸變
textGradient.getPaint().setShader(mLinearGradient);//設(shè)置漸變色
textGradient.invalidate();
文字跑馬燈
<TextView
android:id="@+id/tv_marquee"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:background="@drawable/textview_background"
android:ellipsize="marquee"
android:scrollHorizontally="true"
android:marqueeRepeatLimit="marquee_forever"
android:paddingHorizontal="16dp"
android:text="當(dāng)那繁華落盡,剩下的是滿地憂傷。"
android:textColor="@color/crimson"
android:textSize="16dp"
android:singleLine="true" />
Java
TextView textView_marquee = (TextView)findViewById(R.id.tv_marquee);
textView_marquee.setFocusable(true);
textView_marquee.setSelected(true);
textView_marquee.setFocusableInTouchMode(true);
想實(shí)現(xiàn)文字跑馬燈,需要設(shè)置如下屬性,如果實(shí)現(xiàn)不了,可能是缺少了某個(gè)屬性。marqueeRepeatLimit="marquee_forever"屬性控制循環(huán)次數(shù)
ellipsize="marquee"scrollHorizontally="true"marqueeRepeatLimit="marquee_forever"singleLine="true"focusable="true"focusableInTouchMode="true"-
textView_marquee.setSelected(true);(Java)
文本環(huán)繞圖片
單個(gè)TextView環(huán)繞圖片通過drawableXxx很容易實(shí)現(xiàn),當(dāng)然也可以通過ImageView實(shí)現(xiàn),這里就先不講了。
TextView
android:id="@+id/tv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingHorizontal="16dp"
android:text="示例文本"
android:layout_gravity="center_horizontal"
android:textSize="16sp"
android:drawableTop="@mipmap/ic_launcher"
android:drawableRight="@mipmap/ic_launcher"/>
- 上環(huán)繞
drawableTop="@mipmap/ic_launcher" - 右環(huán)繞
drawableRight="@mipmap/ic_launcher" - 底環(huán)繞
drawableBottom="@mipmap/ic_launcher" - 左環(huán)繞
drawableLeft="@mipmap/ic_launcher"
結(jié)束語
本文講解了TextView的一些簡單應(yīng)用,希望能幫到你。如果發(fā)現(xiàn)有什么錯(cuò)誤的地方,歡迎留言指出,謝謝閱讀。