codecamp

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!" />

null


在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);
    }
}
null
Android UI教程 - Android选项菜单
Android UI教程 - Android RelativeLayout
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }