Android下有众多的Widget,而TextView可以说是最基础的,或者说是最基本的的控件。和其它编程语言相同的label标签一样,一般用来显示固定长度的文本字符串。
一、实例
在介绍前先演示一下实例
效果图一如上
布局文件text_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/textView_content" /> </LinearLayout>
java代码TextViewActivity.java
package com.example.test; import android.app.Activity; import android.os.Bundle; public class TextViewActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.text_view); } }
再在AndroidManifest.xml配置一下<activity android:name="TextViewActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
其中用的字符串资源是
<string name="textView_content">我是Textview,大家要好好学习!</string>
二、TextView类结构
java.lang.Object
↳ android.view.View
↳ android.widget.TextView
直接子类
Button, CheckedTextView, Chronometer, DigitalClock, EditText, TextClock
间接子类
AutoCompleteTextView, CheckBox, CompoundButton, ExtractEditText, MultiAutoCompleteTextView, RadioButton, Switch, ToggleButton
Button,EditText,CheckBox,RadioButton,ToggleButton 等都是它的直接子类或间接子类,所以我们要一定学好TextView,才能更好地学习其它控件。
三、TextView属性
XML Attributes Attribute Name Related Method Description android:autoLink setAutoLinkMask(int) Controls whether links such as urls and email addresses are automatically found and converted to clickable links. android:autoText setKeyListener(KeyListener) If set, specifies that this TextView has a textual input method and automatically corrects some common spelling errors. android:bufferType setText(CharSequence,TextView.BufferType) Determines the minimum type that getText() will return. android:capitalize setKeyListener(KeyListener) If set, specifies that this TextView has a textual input method and should automatically capitalize what the user types. android:cursorVisible setCursorVisible(boolean) Makes the cursor visible (the default) or invisible. android:digits setKeyListener(KeyListener) If set, specifies that this TextView has a numeric input method and that these specific characters are the ones that it will accept. android:drawableBottom setCompoundDrawablesWithIntrinsicBounds(int,int,int,int) The drawable to be drawn below the text. android:drawableEnd setCompoundDrawablesRelativeWithIntrinsicBounds(int,int,int,int) The drawable to be drawn to the end of the text. android:drawableLeft setCompoundDrawablesWithIntrinsicBounds(int,int,int,int) The drawable to be drawn to the left of the text.