天天看點

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();
//設定顔色,三種方式。
        textView.setTextColor();
        textView.setTextColor(Color.BLUE);
        textView.setTextColor(Color.argb(, , , ));
// 通過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");
           

效果如圖(點選可以撥出号碼):

Android UI基礎——TextView控件

3、文本行數

android:lines=”“:設定文本的行數,如果設定為1,就說明隻有一行可以輸入文字,如果文字多出的話,多餘的文字會出屏。

android: maxLines=”“:設定文本的最大行數,寫的行數少于最大行數隻顯示寫有文本的行,如果多于設定的行數,多餘的文字出屏。

4、添加圖檔

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

此例是将圖檔添加到文字的上方,效果圖如下:

Android UI基礎——TextView控件

5、省略顯示文本

省略顯示文本一般使用在文本過多而不需要全部顯示或者需要文本滾動顯示效果的時候。

在TextView中可以設定 android:ellipsize屬性,有四個屬性值:marquee(滾動顯示),end(末尾顯示······),middle(中間顯示······),none(不顯示省略号,多餘自動不顯示)。

但是滾動顯示“marquee”還需要設定焦點,代碼如下:

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

效果圖如下:

Android UI基礎——TextView控件

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(,,drawable.getMinimumWidth(),drawable.getMinimumHeight());
                        } catch (NoSuchFieldException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                        return drawable;
                    }
                },null);
                textView.setText(spanned1);
            }
        });
    }

}
           

效果如下:

Android UI基礎——TextView控件