這篇文章主要介紹了Android開發中TextView各種常見使用方法,結合實例形式總結分析了Android開發中TextView各種常見佈局與功能實現技巧,需要的朋友可以參考下
本文實例講述了Android開發中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,文本框結尾處繪製圖片--> <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="郵箱是 [email protected], 電話是 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實現切換
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相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方法彙總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android佈局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。