Android開發(fā)中TextView各種常見使用方法小結(jié)
本文實(shí)例講述了Android開發(fā)中TextView各種常見使用方法。分享給大家供大家參考,具體如下:
效果圖:

XML布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/root"
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:orientation="vertical">
<!--設(shè)置字號20sp,文本框結(jié)尾處繪制圖片-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我愛Java"
android:textSize="20pt"
android:drawableRight="@drawable/ic_dashboard_black_24dp" />
<!--設(shè)置中間省略,所有字母大寫-->
<!--android:ellipsize="middle" ···省略號居中顯示-->
<!--android:textAllCaps="true"全體大寫-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="我愛Java我愛Java我愛Java我愛Java我愛Java我愛Java我愛Java我愛Java"
android:ellipsize="middle"
android:textAllCaps="true"/>
<!--對郵件電話、添加鏈接-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="郵箱是 814484626@qq.com, 電話是 13100000000"
android:autoLink="email|phone"/>
<!--設(shè)置顏色、大小、并使用陰影-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="測試文字"
android:textSize="25pt"
android:shadowColor="#00f"
android:shadowDx="10.0"
android:shadowDy="8.0"
android:shadowRadius="3.0"
android:textColor="#f00"/>
<!--測試密碼框-->
<TextView
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textSize="25sp"
android:password="true"/>
<!--測試密碼框-->
<CheckedTextView
android:id="@+id/check_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="可勾選的文本"
android:textSize="25sp"
android:checkMark="@xml/check"/>
<!--通過Android:background指定背景-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="帶邊框的文本"
android:textSize="25sp"
android:background="@drawable/bg_border"/>
<!--通過Android:drawableLeft繪制一張圖片-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="圓角邊框,漸變背景的文本"
android:textSize="25sp"
android:background="@drawable/bg_border2"/>
</LinearLayout>
bg_bordor
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!--設(shè)置背景色為透明色--> <solid android:color="#0000"/> <!--設(shè)置紅色邊框--> <stroke android:width="4px" android:color="#f00"/> </shape>
bg_bordor2
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--制定圓角矩形的四個圓角半徑-->
<corners android:topLeftRadius="30px"
android:topRightRadius="5px"
android:bottomRightRadius="30px"
android:bottomLeftRadius="5px"/>
<!--指定邊框線條的寬度和顏色-->
<stroke android:width="4px" android:color="#f0f"/>
<!--指定使用漸變背景色,使用sweep類型漸變
顏色從 紅色->綠色->藍(lán)色-->
<gradient android:startColor="#f00"
android:centerColor="#0f0"
android:endColor="#00f"
android:type="sweep"/>
</shape>
勾選效果通過xml selector實(shí)現(xiàn)切換
android:checkMark="@xml/check"/>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/ok" android:state_selected="true"/> <item android:drawable="@drawable/no" android:state_selected="false"/> </selector>
Java代碼添加點(diǎn)擊事件
public class Home extends AppCompatActivity {
private int i = 0 ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//顯示manLayout
final CheckedTextView checkedTextView = (CheckedTextView) findViewById(R.id.check_text_view);
checkedTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( i++ % 2 == 1 ){
checkedTextView.setSelected(true);
}
else {
checkedTextView.setSelected(false);
}
}
});
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- Android文本視圖TextView實(shí)現(xiàn)跑馬燈效果
- Android文本視圖TextView實(shí)現(xiàn)聊天室效果
- Android使用TypeFace設(shè)置TextView的文字字體
- Flutter中嵌入Android 原生TextView實(shí)例教程
- android使用TextView實(shí)現(xiàn)跑馬燈效果
- Android開發(fā)實(shí)現(xiàn)TextView超鏈接5種方式源碼實(shí)例
- android TextView中識別多個url并分別點(diǎn)擊跳轉(zhuǎn)方法詳解
- Android開發(fā)之TextView使用intent傳遞信息,實(shí)現(xiàn)注冊界面功能示例
- Android使用AutoCompleteTextView實(shí)現(xiàn)自動填充功能的案例
- Android為textView設(shè)置setText的時候報(bào)錯的講解方案
- 詳解Android TextView屬性ellipsize多行失效的解決思路
- 在Android TextView中顯示圖片的4種方式詳解
- Android實(shí)現(xiàn)梯形TextView效果
相關(guān)文章
Android自定義webView頭部進(jìn)度加載效果
這篇文章主要介紹了Android自定義webView頭部進(jìn)度加載效果,小編畫一條進(jìn)度線,然后加載webview上面,具體實(shí)現(xiàn)代碼大家參考下本文2017-11-11
android實(shí)現(xiàn)獲取有線和無線Ip地址的方法
這篇文章主要介紹了android實(shí)現(xiàn)獲取有線和無線Ip地址的方法,較為詳細(xì)的分析了Android獲取IP地址的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
Android Studio實(shí)現(xiàn)QQ的注冊登錄和好友列表跳轉(zhuǎn)
最近做了一個項(xiàng)目,這篇文章主要介紹了Android Studio界面跳轉(zhuǎn),本次項(xiàng)目主要包含了注冊、登錄和好友列表三個界面以及之間相互跳轉(zhuǎn),感興趣的可以了解一下2021-05-05
Android編程實(shí)現(xiàn)Toast自定義布局簡單示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)Toast自定義布局的方法,結(jié)合簡單實(shí)例形式分析了Toast自定義布局的實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
Android table布局開發(fā)實(shí)現(xiàn)簡單計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android table布局開發(fā)實(shí)現(xiàn)簡單計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
一文帶你深入理解Android Window系統(tǒng)
Android中的窗口系統(tǒng)是應(yīng)用程序用戶界面的核心組件之一,它負(fù)責(zé)管理可視化區(qū)域、處理用戶輸入事件以及與系統(tǒng)UI交互,本文將深入介紹與Android窗口系統(tǒng)相關(guān)的重要概念,需要的朋友可以參考下2023-10-10
[Alibaba-ARouter]淺談簡單好用的Android頁面路由框架
這篇文章主要介紹了[Alibaba-ARouter]淺談簡單好用的Android頁面路由框架,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

