閱讀53 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Android中的基本控件(上)--TextView控件

TextView組件的主要功能是用於顯示文本,此類定義。

java.lang.Object
  ↳android.view.View
  ↳android.widget.TextView

<TextView>組件的常用屬性及對應方法 


<TextView 						定義文本顯示框組件
		android: 			定義此文本組件的ID
		android:layout_width="fill_parent"			寬度為整個容器的寬度
		android:layout_height="wrap_content" 		高度為文字高度
		android:textColor="#FFFF00"			文字顏色黃色的RGB碼
		android:textSize="12pt"				設置文字大小為12像素
		android:text="文本組件" />		設置默認的顯示文本

<TextView 						定義文本顯示框組件
		android: 				定義此文本組件的ID
		android:layout_width="fill_parent"			寬度為整個容器的寬度
		android:layout_height="wrap_content" 			高度為文字高度
		android:text="網址:www.mldnjava.cn"			默認的文本信息
		android:layout_margin="30dip" />			距離左邊30個像素的距離

<TextView 					定義文本顯示框組件
		android: 			定義此文本組件的ID,為Activity程序使用
		android:layout_width="fill_parent"		寬度為整個容器的寬度
		android:layout_height="wrap_content" 		高度為文字高度
		android:text="李興華老師"			設置顯示文字
		android:layout_marginTop="10px"		設置距離上邊控件距離為10像素
		android:maxLength="3" />			隻顯示3個長度文字

<TextView 						定義文本顯示框組件
		android: 				定義此文本組件的ID
		android:layout_width="wrap_content"			寬度為圖片寬度
		android:layout_height="wrap_content" 			高度為圖片高度
		android:background="@drawable/logo"			將文本框的背景設置為圖片
		android:text="這是在背景上的文字信息"			設置顯示文字
		android:textStyle="bold"				設置為粗體文字
		android:textColor="#000000" />			文字顏色為黑色

定義布局管理器,增加鏈接顯示功能 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 						定義線型布局管理器
	xmlns:andro
	android:orientation="vertical" 			所有組件垂直擺放
	android:layout_width="fill_parent"			布局管理器寬度為屏幕寬度
	android:layout_height="fill_parent">			布局管理器高度為屏幕高度
	<TextView						定義文本組件
		android:				組件ID,程序中使用
		android:layout_width="fill_parent"			組件寬度為屏幕寬度
		android:layout_height="fill_parent"		組件高度為屏幕高度
		android:autoLink="all"				如果有網址則進行顯示
		android:textColor="#FFFF00"			文字顏色為黃色
		android:textSize="45px"				文字大小為45像素
		android:text="網址:www.mldnjava.cn"/>		默認文字
</LinearLayout>

使用樣式表文件

在Android中為了方便美工對組件進行修飾,也可以使用一些樣式文件對組件顯示進行控製,用戶隻需要按照如下的xml文件格式即可定義組件的顯示樣式,格式如下: 

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<style name="樣式名稱" parent="父樣式表">
		<item name="定義的屬性">屬性內容</item>
	</style>
</resources>

定義樣式文件 —— values/styles.xml 

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<style name="msg_style">				定義樣式文件
	<item name="android:textSize">45px</item>	文字大小為45像素
		<item name="android:textColor">#FFFF00</item>文字顏色設置為黃色
		<item name="android:autoLink">all</item>	顯示文本中的鏈接
		<item name="android:layout_width">fill_parent</item>組件寬度為屏幕寬度
		<item name="android:layout_height">wrap_content</item>組件高度為文字高度
	</style>
</resources>
定義布局管理器 —— main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 					定義線型布局管理器
	xmlns:andro
	android:orientation="vertical" 		所有組件垂直擺放
	android:layout_width="fill_parent"		布局管理器寬度為屏幕寬度
	android:layout_height="fill_parent">		布局管理器高度為屏幕高度
	<TextView					定義文本顯示組件
		android:			組件ID,程序中使用
					定義組件的樣式文件
		android:text="網址:www.mldnjava.cn"/>		組件的默認顯示文字
</LinearLayout>



最後更新:2017-04-03 12:56:08

  上一篇:go ASp.net 剖析三層架構
  下一篇:go Pig源碼分析: 邏輯執行計劃模塊