Android基礎(chǔ)到進(jìn)階UI TextView介紹+實(shí)例

TextView是什么

向用戶顯示文本螺句,并可選擇允許他們編輯文本虽惭。TextView是一個(gè)完整的文本編輯器,但是基類為不允許編輯蛇尚;其子類EditText允許文本編輯芽唇。

咱們先上一個(gè)圖看看TextView的繼承關(guān)系:

image

從上圖可以看出TxtView繼承了View,它還是Button、EditText等多個(gè)組件類的父類匆笤。咱們看看這些子類是干嘛的研侣。

  • Button:用戶可以點(diǎn)擊或單擊以執(zhí)行操作的用戶界面元素。
  • CheckedTextView:TextView支持Checkable界面和顯示的擴(kuò)展炮捧。
  • Chronometer:實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器的類庶诡。
  • DigitalClock:API17已棄用可用TextClock替代。
  • EditText:用于輸入和修改文本的用戶界面元素咆课。
  • TextClock:可以將當(dāng)前日期和/或時(shí)間顯示為格式化字符串末誓。

看看他的兒子都這么牛掰,何況是爸爸书蚪,今天咱就看看這個(gè)爸爸級(jí)組件:TextView喇澡。

使用TextView

1.在xml中創(chuàng)建并設(shè)置屬性

image

咱們看上圖說(shuō)話。上圖的文字顯示多種多樣殊校,但是也僅包含TextView的部分功能晴玖,看看這多種多樣的顯示也是比較有意思的。

下面咱看看代碼實(shí)踐:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/dimen_20"
    android:orientation="vertical">
    <!--在Design中表示可從左側(cè)控件展示處拖拽至布局文件上为流,創(chuàng)建簡(jiǎn)單一個(gè)TextView呕屎。-->
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
    <!--修改顏色、大小-->
    <!--設(shè)置顏色 @color/color_ff0000位置:app/values/colors-->
    <!--設(shè)置大小 @dimen/text_size_18位置:app/values/dimens-->
    <!--設(shè)置內(nèi)容 @string/str_setting_color_size位置:app/values/strings-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/str_setting_color_size"
        android:layout_marginTop="@dimen/dimen_10"
        android:textColor="@color/color_ff0000"
        android:textSize="@dimen/text_size_20" />
    <!--添加圖片和使用陰影-->
    <!--添加圖片:drawableTop艺谆、drawableBottom榨惰、drawableLeft(drawableStart)、drawableRight(drawableEnd)-->
    <!--使用陰影:shadowColor(陰影顏色)静汤、shadowDx(tv_2位置為基準(zhǔn),數(shù)字越大越往右)居凶、
    shadowDy(tv_2位置為基準(zhǔn)虫给,數(shù)字越大越往下)、shadowRadius(數(shù)字越大越模糊)-->
    <!--圖片 @mipmap/ic_launcher 位置:app/mipmap/任意一個(gè)目錄能找到即可-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@mipmap/ic_launcher"
        android:layout_marginTop="@dimen/dimen_10"
        android:gravity="center_vertical"
        android:shadowColor="@color/color_FF773D"
        android:shadowDx="30"
        android:shadowDy="-20"
        android:shadowRadius="2"
        android:text="右側(cè)添加圖片和使用陰影"
        android:textColor="@color/color_188FFF"
        android:textSize="@dimen/text_size_20" />
    <!--對(duì)電話和郵件增加鏈接-->
    <!--autoLink對(duì)文本內(nèi)容自動(dòng)添加E-mail地址侠碧、電話號(hào)碼添加超級(jí)鏈接-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autoLink="email|phone"
        android:gravity="center_vertical"
        android:layout_marginTop="@dimen/dimen_10"
        android:text="可點(diǎn)擊跳轉(zhuǎn)郵件:SCC5201314@qq.com\n可點(diǎn)擊跳轉(zhuǎn)電話:0215201314"
        android:textColor="@color/color_188FFF"
        android:textSize="@dimen/text_size_14" />
    <!--內(nèi)容過(guò)多-->
    <!--maxLength最多顯示幾行抹估,單行也可用android:singleline="true"-->
    <!--ellipsize,內(nèi)容顯示不下時(shí)弄兜,顯示...(位置最前药蜻、中間、最后都可以)替饿,這里要加行數(shù)限制才行-->
    <!--lineSpacingMultiplier语泽,行距-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:gravity="center_vertical"
        android:lineSpacingMultiplier="1.2"
        android:layout_marginTop="@dimen/dimen_10"
        android:maxLength="2"
        android:text="TxtView繼承了View,它還是Button视卢、EditText兩個(gè)UI組件類的父類踱卵。它的作用是在用戶界面上顯示文本素。從功能上來(lái)看TextView就是個(gè)文本編輯器据过,只不過(guò)Android關(guān)閉的它的可編輯功能惋砂。如果需要一個(gè)可編輯的文本框妒挎,就要使用到它的子類Editext了,Editext允許用戶編輯文本框中的內(nèi)容西饵。TextView和Editext它倆最大的區(qū)別就在于TextView不允許用戶編輯文本內(nèi)容酝掩,Editext允許用戶編輯文本內(nèi)容。
下面咱寫幾個(gè)實(shí)例來(lái)詳細(xì)了解一下TextView的眷柔。"
        android:textColor="@color/color_188FFF"
        android:textSize="@dimen/text_size_14" />
    <!--background設(shè)置背景色-->
    <!--padding內(nèi)邊距(邊到可用范圍的距離)-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/color_ff0000"
        android:layout_marginTop="@dimen/dimen_10"
        android:padding="10dp"
        android:text="背景色紅色的文本"
        android:textColor="@color/white" />

    <!--帶邊框的文本-->
    <!--layout_margin外邊距(TextView到其他控件的距離)-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dimen_10"
        android:background="@drawable/bg_tv_frame_red"
        android:padding="10dp"
        android:text="帶著紅色邊框的文本" />
    <!--帶邊框的文本背景色漸變-->
    <!--代碼可實(shí)現(xiàn)文本的漸變-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dimen_10"
        android:background="@drawable/bg_tv_frame_gradient"
        android:padding="10dp"
        android:textColor="@color/white"
        android:text="帶著邊框和背景色漸變的文本" />
    
</LinearLayout>

background設(shè)置邊框的文件
android:background="@drawable/bg_tv_frame_red"

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--radius四個(gè)圓角統(tǒng)一設(shè)置庸队,也可以單獨(dú)對(duì)某一個(gè)圓角設(shè)置。例:topLeftRadius-->
    <corners android:radius="2dp"/>
    <!--邊框?qū)挾葁idth闯割、顏色color-->
    <stroke android:width="4px" android:color="@color/color_ff0000" />
</shape>

帶著邊框和背景色漸變
android:background="@drawable/bg_tv_frame_gradient"

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--radius四個(gè)圓角統(tǒng)一設(shè)置彻消,也可以單獨(dú)對(duì)某一個(gè)圓角設(shè)置。例:topLeftRadius-->
    <corners android:radius="8dp"/>
    <!--邊框?qū)挾葁idth宙拉、顏色color-->
    <stroke android:width="1dp" android:color="@color/color_ff0000" />
    <!--漸變的顏色設(shè)置開始到結(jié)束-->
    <gradient
        android:startColor="@color/color_188FFF"
        android:centerColor="@color/color_FF773D"
        android:endColor="@color/color_ff0000"
        android:type="linear"
        />
</shape>

2.在xml中創(chuàng)建,在代碼中設(shè)置屬性

image
  • 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_margin="@dimen/dimen_20"
   android:orientation="vertical">
   <TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="下面是用代碼實(shí)現(xiàn)效果"
       android:textSize="@dimen/text_size_18"
       android:layout_marginTop="@dimen/dimen_20"
       android:layout_marginBottom="@dimen/dimen_10"
       android:textColor="@color/black"
       android:textStyle="bold" />

   <TextView
       android:id="@+id/tv_flag"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:textColor="@color/color_188FFF"
       android:layout_marginTop="@dimen/dimen_10"
       android:text="給文本加劃線"
       android:textSize="@dimen/text_size_18" />

   <TextView
       android:id="@+id/tv_gradient"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginTop="@dimen/dimen_10"
       android:textColor="@color/white"
       android:text="文字漸變是不是很神奇"
       android:textSize="@dimen/text_size_18" />

   <TextView
       android:id="@+id/tv_bg"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginTop="@dimen/dimen_10"
       android:padding="10dp"
       android:text="設(shè)置背景色"
       android:textColor="@color/white"
       android:textSize="@dimen/text_size_18" />

   <TextView
       android:id="@+id/tv_size"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginTop="@dimen/dimen_10"
       android:textColor="@color/color_ff0000"
       android:text="文字特別大小不一致" />

   <TextView
       android:id="@+id/tv_onclick"
       android:layout_width="match_parent"
       android:layout_marginTop="@dimen/dimen_10"
       android:layout_height="wrap_content"
       android:textSize="@dimen/dimen_20"
       android:text="可點(diǎn)擊可長(zhǎng)按" />
</LinearLayout>
  • 運(yùn)行結(jié)果
image
  • 在代碼中實(shí)現(xiàn)
        //下劃線并加清晰
        tv_flag.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        tv_flag.getPaint().setAntiAlias(true);//抗鋸齒

        int[] colors = {0xff188fff, 0xffff773D, 0xffff0000};//顏色的數(shù)組
        LinearGradient mLinearGradient = new LinearGradient(0, 0, 0, 
                tv_gradient.getPaint().getTextSize(), colors, null, Shader.TileMode.CLAMP);
        tv_gradient.getPaint().setShader(mLinearGradient);
        tv_gradient.invalidate();

        int fillColor = Color.parseColor("#ff0000");//內(nèi)部填充顏色
        GradientDrawable gd = new GradientDrawable();//創(chuàng)建drawable
        gd.setColor(fillColor);//設(shè)置背景色
        gd.setCornerRadius(10);//設(shè)置圓角
        tv_bg.setBackground(gd);//設(shè)置背景

        Spannable wordtoSpan = new SpannableString(tv_size.getText().toString());
        //setSpan:參數(shù)1宾尚,設(shè)置文字大小谢澈;參數(shù)2,開始的文字位置煌贴;參數(shù)3,結(jié)束改變文字位置不包含這個(gè)位置
        wordtoSpan.setSpan(new AbsoluteSizeSpan(DensityUtil.dip2px(this, 18)), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        wordtoSpan.setSpan(new AbsoluteSizeSpan(DensityUtil.dip2px(this, 24)), 2, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        wordtoSpan.setSpan(new AbsoluteSizeSpan(DensityUtil.dip2px(this, 10)), 5, tv_size.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_size.setText(wordtoSpan);

        //TextView其實(shí)也是有點(diǎn)擊事件的畢竟它的爸爸Veiew
        tv_onclick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MLog.e("這里是點(diǎn)擊事件");
                Toast.makeText(TextViewActivity.this,"這里是點(diǎn)擊事件",Toast.LENGTH_SHORT).show();
            }
        });
        tv_onclick.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                MLog.e("這里長(zhǎng)按事件");
                Toast.makeText(TextViewActivity.this,"這里長(zhǎng)按事件",Toast.LENGTH_SHORT).show();
                //true表示事件已消費(fèi)
                return true;
            }
        });
  • 運(yùn)行結(jié)果分析

    • TextView的屬性在xml中可以使用的大部分在代碼中也是可以實(shí)現(xiàn)的,看個(gè)人喜好怎么去使用锥忿。
    • 因TextView繼承View牛郑,所以可以使用View的方法。如View.OnClickListener()和View.OnLongClickListener()還有去慢慢探索吧敬鬓。

3.在代碼中創(chuàng)建并設(shè)置屬性

  • 先看效果圖:
image
  • 下面是實(shí)現(xiàn)所用的代碼:
  //ll_act_tv布局文件根布局id
  LinearLayout ll_act_tv = findViewById(R.id.ll_act_tv);
  TextView textView = new TextView(this);//創(chuàng)建控件
  textView.setText("蠢代碼寫的哦");//設(shè)置控件內(nèi)容
  textView.setTextColor(Color.RED);//設(shè)置控件顏色
  textView.setTextSize(DensityUtil.dip2px(this, 20));//設(shè)置控件字體大小
  ll_act_tv.addView(textView);

TextView今天就聊到這里淹朋,后面還有它的子類,比較子類也是比較厲害的不可能一文搞定钉答。你學(xué)會(huì)了嗎础芍?嘿嘿嘿

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市数尿,隨后出現(xiàn)的幾起案子仑性,更是在濱河造成了極大的恐慌,老刑警劉巖右蹦,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件诊杆,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡何陆,警方通過(guò)查閱死者的電腦和手機(jī)晨汹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)甲献,“玉大人宰缤,你說(shuō)我怎么就攤上這事。” “怎么了慨灭?”我有些...
    開封第一講書人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵朦乏,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我氧骤,道長(zhǎng)呻疹,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任筹陵,我火速辦了婚禮刽锤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘朦佩。我一直安慰自己并思,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開白布语稠。 她就那樣靜靜地躺著宋彼,像睡著了一般。 火紅的嫁衣襯著肌膚如雪仙畦。 梳的紋絲不亂的頭發(fā)上输涕,一...
    開封第一講書人閱讀 51,631評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音慨畸,去河邊找鬼莱坎。 笑死,一個(gè)胖子當(dāng)著我的面吹牛寸士,可吹牛的內(nèi)容都是我干的檐什。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼碉京,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼厢汹!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起谐宙,我...
    開封第一講書人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎界弧,沒想到半個(gè)月后凡蜻,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡垢箕,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年划栓,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片条获。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡忠荞,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情委煤,我是刑警寧澤堂油,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站碧绞,受9級(jí)特大地震影響府框,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜讥邻,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一迫靖、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧兴使,春花似錦系宜、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至欠母,卻和暖如春欢策,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背赏淌。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工踩寇, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人六水。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓俺孙,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親掷贾。 傳聞我的和親對(duì)象是個(gè)殘疾皇子睛榄,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355

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