Android UI基礎——TextView控件

TextView作用很簡單,就是在界面上顯示文本。
TextView直接繼承了View,它是Button、EditText的父類。作用就是在界面上顯示文本。TextView提供了大量的xml屬性,這些屬性大部分不僅可適用於TextView,也可以使用其子類。

接下來對其屬性進行具體的描述:

1、文字屬性的編輯:

設置這些屬性的時候有兩種表示方法:第一種可以在layout的佈局文件中進行設置,第二種可以在Activity的onCreate()方法中進行設置。

屬性 layout中的設置 Activity中的設置
顯示內容 android:text=”“ setText()
字體顏色 android:textColor=”“ setTextColor(Color.rgb( , , ,))
更改大小 android:textSize=”“ setTextSize()

(1)在layout佈局xml 文件設置:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="這是一個TextView文本"
        android:textColor="#000000"
        android:textSize="20dp"
        />

(2)在Activity中設置

//設置文字大小 ,大小隻能傳像素值
        textView.setTextSize(30);
//設置顏色,三種方式。
        textView.setTextColor(0xff00ff00);
        textView.setTextColor(Color.BLUE);
        textView.setTextColor(Color.argb(0x99, 0xff, 0x00, 0x00));
// 通過values中的color中設置的顏色來設定。
        int color = getResources().getColor(R.color.red);
        textView.setTextColor(color);

2、超鏈接

該屬性同樣可以在layout佈局和Activity中設置。
(1)在layout佈局xml 文件設置:
鏈接設置可以設置爲:all, map, none, email, phone以及web。此處以phone爲例:

<TextView
          android:id="@+id/textView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="電話號碼:18189536898"
          android:autoLink="phone"/>

(2)在Activity中設置(在onCreate()方法中設置)

   textView.setAutoLinkMask(Linkify.ALL);
   textView.setText("電話號碼:18189536898");

效果如圖(點擊可以撥出號碼):
這裏寫圖片描述

3、文本行數

android:lines=”“:設置文本的行數,如果設置爲1,就說明只有一行可以輸入文字,如果文字多出的話,多餘的文字會出屏。
android: maxLines=”“:設置文本的最大行數,寫的行數少於最大行數只顯示寫有文本的行,如果多於設置的行數,多餘的文字出屏。

4、添加圖片

在TextView中可以添加圖片,也可以調整圖片的位置。

 android:drawableTop="@mipmap/ic_launcher"

此例是將圖片添加到文字的上方,效果圖如下:
這裏寫圖片描述

5、省略顯示文本

省略顯示文本一般使用在文本過多而不需要全部顯示或者需要文本滾動顯示效果的時候。
在TextView中可以設置 android:ellipsize屬性,有四個屬性值:marquee(滾動顯示),end(末尾顯示······),middle(中間顯示······),none(不顯示省略號,多餘自動不顯示)。
但是滾動顯示“marquee”還需要設置焦點,代碼如下:

android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"

效果圖如下:
這裏寫圖片描述

6、富文本

富文本就是一些特殊的文字或者帶圖片的特殊顯示,比如qq聊天中帶表情的文字對話就是富文本。代碼如下:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.text);
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String html2 = "你好<img src='header'></img>,我也是有圖<img src='ic_launcher'></img>的<font color='#E61A5F'>文本</font>";
                Spanned spanned1 = Html.fromHtml(html2, new Html.ImageGetter() {
                    @Override
                    public Drawable getDrawable(String source) {
                        Drawable drawable = null;
                        //反射機制
                        Class clazz = R.mipmap.class;
                        try {
                            Field field = clazz.getDeclaredField(source);
                            int id = field.getInt(null);
                            drawable = getResources().getDrawable(id);
                            drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
                        } catch (NoSuchFieldException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                        return drawable;
                    }
                },null);
                textView.setText(spanned1);
            }
        });
    }

}

效果如下:
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章