Android UI教程 - Android TextView

2018-01-09 14:51 更新

Android UI教程 - Android TextView


TextView 視圖用于向用戶顯示文本。這是最基本的視圖和一個(gè)你將在您開(kāi)發(fā)Android應(yīng)用程序時(shí)頻繁使用。

如果您需要允許用戶編輯文本顯示,應(yīng)該使用 TextView , EditText 的子類。

在一些其他平臺(tái)中, TextView 通常稱為標(biāo)簽視圖。 它的唯一目的是在屏幕上顯示文本。

例子

以下布局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設(shè)置文本

下面的示例演示如何在XML中設(shè)置 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 hgci.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
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)