Android UI系列:TextView显示文本

本文详细介绍了Android中TextView的基础知识和高级用法,包括TextView的实例展示、类结构、属性设置、代码创建、走马灯效果、创建上下文链接、资源获取与格式化、显示图片以及属性详解,帮助开发者更好地理解和运用这一核心控件。
摘要由CSDN通过智能技术生成

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. 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值