日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

【Android 应用开发】Android UI 设计之 TextView EditText 组件属性方法最详细解析

發布時間:2025/6/17 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Android 应用开发】Android UI 设计之 TextView EditText 组件属性方法最详细解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

.

作者?:萬境絕塵?

轉載請注明出處?:?http://blog.csdn.net/shulianghan/article/details/18964835

.



TextView 相關類的繼承結構 :?

-- 常用的組件 : TextView 直接繼承View類, 同時是 EditText 和 Button 兩組組件類的父類;?





一. TextView詳解


1. TextView文本鏈接相關XML屬性方法


(1) 設置單個連接


文本轉鏈接 : 將指定格式的文本轉換成可單擊的超鏈接形式;

-- XML屬性 : android:autoLink, 該屬性有屬性值 : none, web, email, phone, map, all;

-- 方法 : setAutoLinkMask(int);

eg :?

<TextView android:id="@+id/tv_auto_link_phone"android:layout_height="wrap_content"android:layout_width="fill_parent"android:autoLink="phone"android:text="18511896990 可單擊的電話鏈接"/><TextView android:id="@+id/tv_auto_link_web"android:layout_height="wrap_content"android:layout_width="fill_parent"android:autoLink="web"android:text="baidu.com 可單擊的網頁鏈接"/>

效果圖 :?

? ? ?


(3) 同時設置多個種類的鏈接


如果一個文本中有多個種類的鏈接, android:autoLink屬性使用"|"分隔, 例如 phone|email|web 等;

如果同時設置所有類型連接轉換, 使用 "all" 屬性即可;


示例 :?

<!-- 如果一個TextView中有多個種類的鏈接, autoLink屬性使用 " | "分隔即可 --><TextView android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="電話 : 18511896990 , 郵件 : 904279436@qq.com , 網址 : baidu.com"android:autoLink="web|email|phone"/>
效果圖 :?



2. 繪制圖像相關XML屬性


繪圖設置 : XML屬性可以指定在TextView文本的 左, 右, 上, 下, 開始, 結尾 處設置圖片, 還可以設置文本 與圖片之間的間距;

--?在文本框四周繪制圖片XML屬性 :?

在文本框左邊繪制指定圖像 :android:drawableLeft;

在文本框右邊繪制指定圖像 :android:drawableRight;

在文本框上邊繪制指定圖像 :android:drawableTop;

在文本框下邊繪制指定圖像 : android:drawableBottom;

--?設置圖片方法 : setCompoundDrawablesWithIntrinsicBounds(drawable,drawable,drawable,drawable);


設置圖片與文本間距 : 相當于圖片距離文字的距離, 注意要帶上單位, 建議單位是dip;

-- XML屬性 :android:drawablePadding;


實例 :?

<!-- 該TextView的四周都有圖片, 四個圖片距離文字有50dip的距離 --><TextView android:id="@+id/tv_adrawable_left_right"android:layout_height="wrap_content"android:layout_width="wrap_content"android:drawableLeft="@android:drawable/sym_action_call"android:drawableRight="@android:drawable/sym_action_call"android:drawableTop="@android:drawable/sym_action_call"android:drawableBottom="@android:drawable/sym_action_call"android:drawablePadding="50dip"android:text="左右上下有圖片"/>
顯示效果圖 :?



3. 顯示省略


單行設置 : 顯示省略的時候, 必須設置文本行數為單行, 才能看出效果, ?android:singleLine 可以設置是否單行顯示;


省略設置 : 當顯示文本超過了TextView長度后處理文本內容的方法;?

-- XML屬性 :android.ellipsize;?

-- XML屬性值 :?

none :不做任何處理;?

start : 文本開始處截斷, 顯示省略號;

middle : 文本中間截斷, 顯示省略號;

end : 文本結尾處截斷, 顯示省略號;

marquee : 使用marquee滾動動畫顯示文本;

-- 設置方法 : setEllipsize();


示例 :?

<!-- 設置android:singleLine屬性單行, 并設置在結尾處截斷 --><TextView android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="電話 : 18511896990 , 郵件 : 904279436@qq.com , 網址 : baidu.com"android:singleLine="true"android:ellipsize="end"/>
效果圖 :?



4. 設置顏色 大小 陰影

?

設置文本顏色 :?

-- XML屬性 :android:textColor, 值是顏色代碼, 也可以是資源文件中的顏色;

-- 方法 : setTextColor().


設置文本大小 :?

-- XML屬性 : android:textSize, 值是float值, 注意帶上單位pt;

-- 方法 : setTextSize(float);


設置陰影 :?

-- XML屬性 :?

設置陰影顏色 : android:shadowColor;

設置陰影水平方向偏移 : android:shadowDx;

設置陰影垂直方向偏移 : android:shadowDy;

設置陰影模糊程度 : android:shadowRadius;

-- 方法 : setShadowLayer(float, float, float, int);


示例 :?

<TextView android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="電話 : 18511896990 , 郵件 : 904279436@qq.com , 網址 : baidu.com"android:textColor="#f00"android:textSize="15pt"android:shadowColor="#00f"android:shadowDx="10"android:shadowDy="8"android:shadowRadius="3"/>


效果圖 :?



5. 顯示的文本為密碼


設置文本框是一個密碼框 : 如果要設置顯示的文本是密碼的話, 那么顯示出來的就是 "." , 不能顯示具體的內容;

-- XML屬性 :android:password, 如果是密碼的話, 設置為true;

-- 方法 :setTransformationMethod(TransformationMethod);


示例 :?

<TextView android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="電話 : 18511896990 , 郵件 : 904279436@qq.com , 網址 : baidu.com"android:password="true"/>

效果圖 :?



6. 可勾選的文本


CheckedTextView介紹?: TextView 派生出一個 CheckedTextView , CheckedTextView 增加了一個checked 狀態, 可以通過調用setChecked(boolean)?方法設置checked狀態, 使用isChecked()方法獲取checked狀態, 還可以通過setCheckMarkDrawable()方法 設置它的勾選圖標;

--XML屬性 :android:checkMark, 屬性值是一個drawable圖片;單選可以設置成?"?android:attr/listChoiceIndicatorSingle" 屬性,多選可以設置成?"?android:attr/listChoiceIndicatorMultiple" 屬性;


示例 :?

<CheckedTextView android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="你是猴子請來的救兵嗎"android:checkMark="@drawable/ok"/>

效果圖 :?




7. 設置TextView文本邊框 背景漸變


使用背景 : TextView 是沒有邊框的, 如果要加上邊框, 可以通過設置TextView的背景添加邊框;

自定義背景: 使用XML文件定義一個drawable圖像, 可以為該Drawable指定背景顏色,邊框顏色,邊框寬度,以及邊框角度,顏色漸變等效果;

.

代碼示例 :?

布局文件代碼 :?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent" android:orientation="vertical"><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="喜當爹"android:textSize="24dp"android:background="@drawable/background_frame"/><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="背景顏色,邊框顏色,邊框寬度,以及邊框角度,顏色漸變等效果"android:textSize="24dp"android:background="@drawable/background_frame2"/></LinearLayout>
資源文件代碼 :?

background_frame.xml :?

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" ><!-- 設置背景為透明色 --><solid android:color="#0000" /><!-- 設置邊框的厚度為4像素, 設置邊框顏色 --><stroke android:width="10px"android:color="#01DF01"/></shape>


background_frame2.xml :?
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"><!-- android:shape="rectangle" 指定該圖形的類型為矩形--><!-- 設置該矩形的四個角的角度弧度 --><corners android:topLeftRadius="20px"android:topRightRadius="5px"android:bottomRightRadius="20px"android:bottomLeftRadius="5px"/><!-- 設置邊框的寬度和顏色 --><stroke android:width="10px"android:color="#F0F"/><!-- 設置背景顏色漸變 從 紅色 -> 綠色 -> 藍色, 漸變的類型為sweep漸變 --><gradient android:startColor="#f00"android:centerColor="#0f0"android:endColor="#00f"android:type="sweep"/></shape>
效果圖 :?



8. 分析Android:layout_width 與 android:width 與 android:minWidth區別及共存策略

.

策略 :?

-- 當android:layout_width為fill_parent的時候, android:width 與 android:minWidth 設置不起作用;

-- 當android:layout_width為warp_content的時候,android:width 與 android:minWidth 單獨設置的時候都起作用, 兩者一起設置android:width起作用;

--當android:layout_width為具體數值的時候,?android:width 與 android:minWidth 都不起作用;


得出結論 :?

三者優先級順序 :?

android:layout_width > android:width > android:minWidth ;?


源碼 :?

<?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:orientation="vertical" ><TextView android:layout_height="wrap_content"android:layout_width="fill_parent"android:background="#DF8326"android:text="寬度填充全部"/><TextView android:layout_width="wrap_content" android:layout_height="10px"/><TextView android:layout_height="wrap_content"android:layout_width="fill_parent"android:width="100dp"android:background="#DF8326"android:text="width不起作用"/><TextView android:layout_width="wrap_content" android:layout_height="10px"/><TextView android:layout_height="wrap_content"android:layout_width="fill_parent"android:minWidth="50dp"android:background="#DF8326"android:text="minWidth不起作用"/><TextView android:layout_width="wrap_content" android:layout_height="10px"/><TextView android:layout_height="wrap_content"android:layout_width="fill_parent"android:width="50dp"android:minWidth="50dp"android:background="#DF8326"android:text="都不起作用"/><TextView android:layout_width="wrap_content" android:layout_height="40px"/><TextView android:layout_height="wrap_content"android:layout_width="wrap_content"android:background="#DF8326"android:text="寬度包裹全部"/><TextView android:layout_width="wrap_content" android:layout_height="10px"/><TextView android:layout_height="wrap_content"android:layout_width="wrap_content"android:width="150dp"android:background="#DF8326"android:text="width起作用"/><TextView android:layout_width="wrap_content" android:layout_height="10px"/><TextView android:layout_height="wrap_content"android:layout_width="wrap_content"android:minWidth="120dp"android:background="#DF8326"android:text="minWidth起作用"/><TextView android:layout_width="wrap_content" android:layout_height="10px"/><TextView android:layout_height="wrap_content"android:layout_width="wrap_content"android:width="150dp"android:minWidth="120dp"android:background="#DF8326"android:text="都有的時候width起作用"/><TextView android:layout_width="wrap_content" android:layout_height="40px"/><TextView android:layout_height="wrap_content"android:layout_width="200dp"android:background="#DF8326"android:text="layout_width為200dp"/><TextView android:layout_width="wrap_content" android:layout_height="10px"/><TextView android:layout_height="wrap_content"android:layout_width="200dp"android:width="150dp"android:background="#DF8326"android:text="layout_width為200dp, width150dp"/><TextView android:layout_width="wrap_content" android:layout_height="10px"/><TextView android:layout_height="wrap_content"android:layout_width="200dp"android:minWidth="120dp"android:background="#DF8326"android:text="layout_width為200dp,minWidth100dp"/><TextView android:layout_width="wrap_content" android:layout_height="10px"/><TextView android:layout_height="wrap_content"android:layout_width="200dp"android:width="150dp"android:minWidth="120dp"android:background="#DF8326"android:text="都有的時候都不起作用"/><TextView android:layout_width="wrap_content" android:layout_height="10px"/></LinearLayout>
效果圖 :?



9. 顯示HTML效果頁面


使用Html.fromHtml("")方法, 參數是html界面內容, 可以使用html標簽設置文本效果;

例如可以使用Html.fromHtml("<font size='20' color='red'>變大, 紅色</font>");

案例 :?

textView.setText(Html.fromHtml("<font color='red'><big>變大, 紅色 </big></font>" +" 正常 " +"<font color='green'><small> 變小, 綠色</small></font> "));
效果 :?




總結 在Android中顯示HTML頁面的方法 :

-- 瀏覽器訪問 :?

Uri uri = Uri.parse("http://blog.csdn.net/shulianghan");Intent intent = new Intent(Intent.ACTION_VIEW, uri);startActivity(intent);
-- 使用TextView顯示 :?

TextView tv_1 = (TextView)findViewById(R.id.tv_1);tv_1.setText(Html.fromHtml("<font size='20' color='#ff0000'>顯示網頁信息</font>"));


-- 使用WebView組件顯示 :?

WebView webview = (WebView) findViewById(R.id.wv);webview.getSettings().setJavaScriptEnabled(true);webview.loadUrl("http://blog.csdn.net/shulianghan");

10. Spannable設置TextView特效


a. 創建Spannable對象 : 使用new?SpannableString("")創建該對象, 傳入想要添加效果的字符串;

b. 為指定范圍的字符串添加效果 :?span.setSpan(new AbsoluteSizeSpan(58), 1, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE), 為下標從 1 ~ 5的字符串添加 字體大小為58像素的效果;

c. 將Spannable對象設置給TextView :?textView.setText(span);


實例 :?

源碼 :?

TextView textView = (TextView) findViewById(R.id.tv_1);Spannable span = new SpannableString("使用Spannable設置字體效果"); span.setSpan(new AbsoluteSizeSpan(58), 1, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); span.setSpan(new ForegroundColorSpan(Color.RED), 1, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); span.setSpan(new BackgroundColorSpan(Color.YELLOW), 1, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(span);
效果圖 :?



?

.

作者?:萬境絕塵?

轉載請注明出處?:?http://blog.csdn.net/shulianghan/article/details/18964835

.


二. EditText屬性詳解


共享屬性 : EditText 與 TextView共享大部分XML屬性, 但是EditText可以接受用戶輸入;

類型定義屬性 : EditText最重要的屬性是android:inputType, 該屬性用來定義輸入的數據類型;?

自動完成功能輸入組件 :AutoCompletetextView, 該組件是帶自動完成功能的組件, 通常與Adapter一起使用;

全屏輸入法 :ExtractEditText, EditText的底層服務類, 負責提供全屏輸入法;


案例 :?

<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" android:stretchColumns="1"><!-- android:stretchColumns 屬性表示 第一列允許被拉伸, 注意索引從0開始android:hint 屬性表示Edittext沒有輸入之前顯示的內容android:selectAllOnFocus 如果文本框的內容可選擇, 當該EditText獲取焦點時是否全部選中內容--><TableRow ><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="用戶名 : "android:textSize="16sp"/><EditText android:layout_width="fill_parent"android:layout_height="wrap_content"android:hint="請填寫登陸賬號"android:selectAllOnFocus="true"/> </TableRow><!-- android:inputType = "numberPassword" 屬性設置該輸入框輸入密碼, 輸入進去的值都顯示 點號--><TableRow ><TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="密碼 : "android:textSize="16sp"/><EditText android:layout_width="fill_parent"android:layout_height="wrap_content"android:inputType="textPassword"/></TableRow><!-- android:inputType = "number" 屬性設置數字--><TableRow ><TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="年齡 : "android:textSize="16sp"/><EditText android:layout_width="fill_parent"android:layout_height="wrap_content"android:inputType="number"/></TableRow><!-- android:inputType = "data" 屬性設置日期--><TableRow ><TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="生日 : "android:textSize="16sp"/><EditText android:layout_width="fill_parent"android:layout_height="wrap_content"android:inputType="date"/></TableRow><!-- android:inputType = ""phone"" 屬性設置電話--><TableRow ><TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="電話 : "android:textSize="16sp"/><EditText android:layout_width="fill_parent"android:layout_height="wrap_content"android:hint="請輸入電話號碼"android:selectAllOnFocus="true"android:inputType="phone"/></TableRow></TableLayout>
效果圖 :?

??


? ?

.

作者?:萬境絕塵?

轉載請注明出處?:?http://blog.csdn.net/shulianghan/article/details/18964835

.


總結

以上是生活随笔為你收集整理的【Android 应用开发】Android UI 设计之 TextView EditText 组件属性方法最详细解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。