1,是什么?
TextView(文本框)涎拉,用于顯示文本的一個(gè)控件瑞侮;
2,基本屬性
1)id:為TextView設(shè)置一個(gè)組件id鼓拧,根據(jù)id半火,我們可以在Java代碼中通過(guò) findViewById()的方法獲取到該對(duì)象,然后進(jìn)行相關(guān)屬性的設(shè)置毁枯;
2)layout_width:組件的寬度慈缔,一般寫:wrap_content或match_parent,前者是控件顯示的內(nèi)容多大种玛,控件就多大藐鹤,而后者會(huì)填滿該控件所在的父容器,當(dāng)然也可以設(shè)置成特定的大小赂韵,比如我這里為了顯示效果娱节,設(shè)置成了200dp;
3)layout_height:組件的寬度祭示,跟layout_width類比使用肄满;
gravity:設(shè)置控件中內(nèi)容的對(duì)齊方向,TextView中是文字,ImageView中是圖片等等稠歉。
4)text:設(shè)置顯示的文本內(nèi)容掰担,一般我們是把字符串寫到string.xml文件中,然后通過(guò)@String/xxx取得對(duì)應(yīng)的字符串內(nèi)容的怒炸;
5)textColor:設(shè)置字體顏色带饱;
6)textStyle:設(shè)置字體風(fēng)格,三個(gè)可選值:normal(無(wú)效果)阅羹,bold(加粗)勺疼,italic(斜體);
7)textSize:字體大小捏鱼,單位sp执庐;
8)background:控件的背景顏色,可以理解為填充整個(gè)控件的顏色导梆,可以是圖片哦轨淌。
3,帶陰影的TextView
1)涉及到的4個(gè)屬性:
android:shadowColor:設(shè)置陰影顏色,需要與shadowRadius一起使用看尼;
android:shadowRadius:設(shè)置陰影的模糊程度,設(shè)為0.1就變成字體顏色了猿诸;
android:shadowDx:設(shè)置陰影在水平方向的偏移,就是水平方向陰影開始的橫坐標(biāo)位置;
android:shadowDy:設(shè)置陰影在豎直方向的偏移,就是豎直方向陰影開始的縱坐標(biāo)位置狡忙。
簡(jiǎn)單示例代碼和效果:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context="com.example.luolu.textviewdemo.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:shadowColor="#181717"
android:shadowDx="10.0"
android:shadowDy="15.0"
android:shadowRadius="5.0"
android:text="帶陰影的TextView"
android:textColor="#070721"
android:textSize="48sp" />
</RelativeLayout>
3,帶邊框的TextView
1)涉及到的shapeDrawable資源文件的幾個(gè)節(jié)點(diǎn)以及屬性
<solid android:color = "xxx"> 設(shè)置背景顏色址芯;
<stroke android:width = "xdp" android:color="xxx"> 設(shè)置邊框的粗細(xì),以及邊框顏色灾茁;
<padding androidLbottom = "xdp"...> 設(shè)置邊距;
<corners android:topLeftRadius="10px"...> 設(shè)置圓角谷炸;
<gradient> 設(shè)置漸變色,可選屬性有: startColor:起始顏色 endColor:結(jié)束顏色 centerColor:中間顏色 angle:方向角度,等于0時(shí),從左到右,然后逆時(shí)針?lè)较蜣D(zhuǎn),當(dāng)angle = 90度時(shí)從下往上 type:設(shè)置漸變的類型北专。
簡(jiǎn)單示例代碼和效果:
drawable -->r1.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2px" android:color="#000000"/>
<gradient
android:angle="270"
android:endColor="#C0C0C0"
android:startColor="#FCD209" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/>
</shape>
drawable -->r2.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#87CEEB" />
<!-- 設(shè)置一個(gè)黑色邊框 -->
<stroke
android:width="2px"
android:color="#000000" />
<!-- 設(shè)置四個(gè)圓角的半徑 -->
<corners
android:bottomLeftRadius="10px"
android:bottomRightRadius="10px"
android:topLeftRadius="10px"
android:topRightRadius="10px" />
<!-- 設(shè)置一下邊距,讓空間大一點(diǎn) -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
Layout -->activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.example.luolu.textviewdemo.MainActivity">
<TextView
android:id="@+id/txtOne"
android:layout_width="250dp"
android:layout_height="64dp"
android:textSize="20sp"
android:gravity="center"
android:background="@drawable/r1"
android:text="矩形邊框的TextView" />
<TextView
android:id="@+id/txtTwo"
android:layout_width="250dp"
android:layout_height="64dp"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:gravity="center"
android:background="@drawable/r2"
android:text="圓角邊框的TextView" />
</LinearLayout>
效果圖:
4,帶圖片的TextView
1)基本用法:
設(shè)置圖片的核心其實(shí)就是:drawableXxx;可以設(shè)置四個(gè)方向的圖片: drawableTop(上),drawableButtom(下),drawableLeft(左),drawableRight(右) 另外,也可以使用drawablePadding來(lái)設(shè)置圖片與文字間的間距旬陡;
實(shí)例代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.example.luolu.textviewdemo.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:drawableTop="@drawable/luolu1"
android:drawableLeft="@drawable/luolu2"
android:drawableRight="@drawable/luolu3"
android:drawableBottom="@drawable/luolu4"
android:drawablePadding="10dp"
android:text="猜猜這是誰(shuí)?"
android:textSize="26sp"/>
</LinearLayout>
效果圖:
5拓颓,跑馬燈效果的TextView
實(shí)例實(shí)現(xiàn)代碼:
<TextView
android:id="@+id/txtOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="Alpha,未來(lái)已來(lái)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!haha。描孟。驶睦。。匿醒。场航。。呵呵呵呵呵廉羔,呵呵呵呵呵gggggggggggggggg呵呵呵呵呵~"/>
效果圖:
6溉痢,設(shè)置TextView字間距和行間距
字間距:
android:textScaleX:控制字體水平方向的縮放,默認(rèn)值1.0f,值是float
Java中setScaleX(2.0f);
行間距: Android系統(tǒng)中TextView默認(rèn)顯示中文時(shí)會(huì)比較緊湊孩饼,為了讓每行保持的行間距
android:lineSpacingExtra:設(shè)置行間距髓削,如"3dp" android:lineSpacingMultiplier:設(shè)置行間距的倍數(shù),如"1.2"
7,自動(dòng)換行
自動(dòng)換行通過(guò) android:singleLine 設(shè)置镀娶,默認(rèn)為 false立膛。
如需要自動(dòng)換行,可以用:
android:singleLine = "false"
如果要在一行顯示完汽畴,不換行旧巾,可以用:
android:singleLine = "true"
如果多行顯示不完,添加maxLines的屬性即可忍些。