Android UI教程 - Android TextView
Android UI教程 - Android TextView
TextView 视图用于向用户显示文本。这是最基本的视图和一个你将在您开发Android应用程序时频繁使用。
如果您需要允许用户编辑文本显示,应该使用 TextView , EditText 的子类。
在一些其他平台中, TextView 通常称为标签视图。 它的唯一目的是在屏幕上显示文本。
例子
以下布局xml资源文件有 TextView 的定义。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
TextView中的参考颜色资源
下面的代码显示了如何TextView中的参考颜色资源
res / values / colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background_color">#aa0000</color>
</resources>
布局xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background_color"
android:gravity="center"
android:text="Hello World, MainActivity!" />
在XML中为TextView设置文本
下面的示例演示如何在XML中设置 TextView 的文本。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Example from www.w3cschool.cn"
/>
</LinearLayout>
Java代码
package com.java2s.app;
// www . j av a2 s.c o m
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}