TextView知識(shí)點(diǎn)總結(jié)和demo實(shí)現(xiàn)

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_contentmatch_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>


image.png

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>
效果圖:


image.png

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>
效果圖:


image.png

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呵呵呵呵呵~"/>
效果圖:


image.png

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的屬性即可忍些。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末鲁猩,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子罢坝,更是在濱河造成了極大的恐慌廓握,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,198評(píng)論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件嘁酿,死亡現(xiàn)場(chǎng)離奇詭異隙券,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)闹司,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門娱仔,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人游桩,你說(shuō)我怎么就攤上這事牲迫。” “怎么了借卧?”我有些...
    開封第一講書人閱讀 167,643評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵盹憎,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我铐刘,道長(zhǎng)陪每,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,495評(píng)論 1 296
  • 正文 為了忘掉前任镰吵,我火速辦了婚禮檩禾,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘疤祭。我一直安慰自己锌订,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評(píng)論 6 397
  • 文/花漫 我一把揭開白布画株。 她就那樣靜靜地躺著辆飘,像睡著了一般啦辐。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蜈项,一...
    開封第一講書人閱讀 52,156評(píng)論 1 308
  • 那天芹关,我揣著相機(jī)與錄音,去河邊找鬼紧卒。 笑死侥衬,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的跑芳。 我是一名探鬼主播轴总,決...
    沈念sama閱讀 40,743評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼博个!你這毒婦竟也來(lái)了怀樟?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,659評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤盆佣,失蹤者是張志新(化名)和其女友劉穎往堡,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體共耍,經(jīng)...
    沈念sama閱讀 46,200評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡虑灰,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了痹兜。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片穆咐。...
    茶點(diǎn)故事閱讀 40,424評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖字旭,靈堂內(nèi)的尸體忽然破棺而出庸娱,到底是詐尸還是另有隱情,我是刑警寧澤谐算,帶...
    沈念sama閱讀 36,107評(píng)論 5 349
  • 正文 年R本政府宣布,位于F島的核電站归露,受9級(jí)特大地震影響洲脂,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜剧包,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評(píng)論 3 333
  • 文/蒙蒙 一恐锦、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧疆液,春花似錦一铅、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)肮之。三九已至,卻和暖如春卜录,著一層夾襖步出監(jiān)牢的瞬間戈擒,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工艰毒, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,798評(píng)論 3 376
  • 正文 我出身青樓霎肯,卻偏偏與公主長(zhǎng)得像逆济,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子绊汹,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,262評(píng)論 25 707
  • 歡迎Follow我的GitHub, 關(guān)注我的CSDN. 其余參考Android目錄. 轉(zhuǎn)載請(qǐng)注明出處:http:/...
    passiontim閱讀 4,770評(píng)論 0 31
  • Android功能強(qiáng)大稽屏,界面華麗,但是眾多的布局屬性就害苦了開發(fā)者灸促,下面這篇文章結(jié)合了網(wǎng)上不少資料.第一類:屬性值...
    HangChen閱讀 4,880評(píng)論 0 24
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程诫欠,因...
    小菜c閱讀 6,440評(píng)論 0 17
  • * * 如果script通過(guò)src的屬性引入了外部的文件,里面的js代碼就不會(huì)執(zhí)行了浴栽。(*****) * 荒叼,標(biāo)簽可...
    狠哇塞的小伙子啊閱讀 232評(píng)論 0 0