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é)果
- 在代碼中實(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ì)了嗎础芍?嘿嘿嘿