Android開發(fā)中TextView各種常見使用方法小結(jié)
本文實例講述了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"> <!--設置字號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" /> <!--設置中間省略,所有字母大寫--> <!--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"/> <!--設置顏色、大小、并使用陰影--> <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"> <!--設置背景色為透明色--> <solid android:color="#0000"/> <!--設置紅色邊框--> <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類型漸變 顏色從 紅色->綠色->藍色--> <gradient android:startColor="#f00" android:centerColor="#0f0" android:endColor="#00f" android:type="sweep"/> </shape>
勾選效果通過xml selector實現(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代碼添加點擊事件
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); } } }); } }
更多關于Android相關內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設計有所幫助。
- Android文本視圖TextView實現(xiàn)跑馬燈效果
- Android文本視圖TextView實現(xiàn)聊天室效果
- Android使用TypeFace設置TextView的文字字體
- Flutter中嵌入Android 原生TextView實例教程
- android使用TextView實現(xiàn)跑馬燈效果
- Android開發(fā)實現(xiàn)TextView超鏈接5種方式源碼實例
- android TextView中識別多個url并分別點擊跳轉(zhuǎn)方法詳解
- Android開發(fā)之TextView使用intent傳遞信息,實現(xiàn)注冊界面功能示例
- Android使用AutoCompleteTextView實現(xiàn)自動填充功能的案例
- Android為textView設置setText的時候報錯的講解方案
- 詳解Android TextView屬性ellipsize多行失效的解決思路
- 在Android TextView中顯示圖片的4種方式詳解
- Android實現(xiàn)梯形TextView效果
相關文章
Java語言讀取配置文件config.properties的方法講解
今天小編就為大家分享一篇關于Java語言讀取配置文件config.properties的方法講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03Android編程使用sax解析xml數(shù)據(jù)的方法詳解
這篇文章主要介紹了Android編程使用sax解析xml數(shù)據(jù)的方法,結(jié)合實例形式詳細分析了Android使用sax解析xml數(shù)據(jù)的操作步驟及界面布局、單元測試等相關操作技巧,需要的朋友可以參考下2017-07-07Android使用AlertDialog創(chuàng)建對話框
這篇文章主要為大家詳細介紹了Android使用AlertDialog創(chuàng)建對話框的方法料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12Android10 App 啟動分析進程創(chuàng)建源碼解析
這篇文章主要為大家介紹了Android10 App啟動分析進程創(chuàng)建源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10Android基于虹軟(ArcSoft)實現(xiàn)人臉識別
人工智能時代快速來臨,其中人臉識別是當前比較熱門的技術,在國內(nèi)也越來越多的運用,例如刷臉打卡,刷臉APP,身份識別,人臉門禁等。本文將為大家介紹Android基于虹軟(ArcSoft)實現(xiàn)人臉識別的demo,快來跟隨小編一起學習吧2021-12-12