Android TextView(文本框)詳解

2023-03-31 13:37 更新

本節(jié)引言:

學習完Android中的六大布局,從本節(jié)開始我們來一個個講解Android中的UI控件,本節(jié)給大家?guī)淼腢I控件是:TextView(文本框),用于顯示文本的一個控件,另外聲明一點,我不是翻譯API文檔,不會一個個屬性的去扣,只學實際開發(fā)中常用的,有用的,大家遇到感覺到陌生的屬性可以查詢對應的API!當然,每一節(jié)開始都會貼這一節(jié)對應API文檔的鏈接:TextView API 好了,在開始本節(jié)內容前,先要介紹下幾個單位:

dp(dip): device independent pixels(設備獨立像素). 不同設備有不同的顯示效果,這個和設備硬件有關,一般我們?yōu)榱酥С諻VGA、HVGA和QVGA 推薦使用這個,不依賴像素。 px: pixels(像素). 不同設備顯示效果相同,一般我們HVGA代表320x480像素,這個用的比較多。 pt: point,是一個標準的長度單位,1pt=1/72英寸,用于印刷業(yè),非常簡單易用; sp: scaled pixels(放大像素). 主要用于字體顯示best for textsize。

1.基礎屬性詳解:

通過下面這個簡單的界面,我們來了解幾個最基本的屬性:

布局代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center"
    android:background="#8fffad">

    <TextView
        android:id="@+id/txtOne"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:gravity="center"
        android:text="TextView(顯示框)"
        android:textColor="#EA5246"
        android:textStyle="bold|italic"
        android:background="#000000"
        android:textSize="18sp" />

</RelativeLayout>

上面的TextView中有下述幾個屬性:

  • id:為TextView設置一個組件id,根據id,我們可以在Java代碼中通過findViewById()的方法獲取到該對象,然后進行相關屬性的設置,又或者使用RelativeLayout時,參考組件用的也是id!
  • layout_width:組件的寬度,一般寫:wrap_content或者match_parent(fill_parent),前者是控件顯示的內容多大,控件就多大,而后者會填滿該控件所在的父容器;當然也可以設置成特定的大小,比如我這里為了顯示效果,設置成了200dp。
  • layout_height:組件的高度,內容同上。
  • gravity:設置控件中內容的對齊方向,TextView中是文字,ImageView中是圖片等等。
  • text:設置顯示的文本內容,一般我們是把字符串寫到string.xml文件中,然后通過@String/xxx取得對應的字符串內容的,這里為了方便我直接就寫到""里,不建議這樣寫?。?!
  • textColor:設置字體顏色,同上,通過colors.xml資源來引用,別直接這樣寫!
  • textStyle:設置字體風格,三個可選值:normal(無效果),bold(加粗),italic(斜體)
  • textSize:字體大小,單位一般是用sp!
  • background:控件的背景顏色,可以理解為填充整個控件的顏色,可以是圖片哦!

2.實際開發(fā)例子

2.1 帶陰影的TextView

涉及到的幾個屬性:

  • android:shadowColor:設置陰影顏色,需要與shadowRadius一起使用哦!
  • android:shadowRadius:設置陰影的模糊程度,設為0.1就變成字體顏色了,建議使用3.0
  • android:shadowDx:設置陰影在水平方向的偏移,就是水平方向陰影開始的橫坐標位置
  • android:shadowDy:設置陰影在豎直方向的偏移,就是豎直方向陰影開始的縱坐標位置

效果圖:

實現代碼:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:shadowColor="#F9F900"
        android:shadowDx="10.0"
        android:shadowDy="10.0"
        android:shadowRadius="3.0"
        android:text="帶陰影的TextView"
        android:textColor="#4A4AFF"
        android:textSize="30sp" />

2.2 帶邊框的TextView:

如果你想為TextView設置一個邊框背景,普通矩形邊框或者圓角邊框!下面可能幫到你! 另外TextView是很多其他控件的父類,比如Button,也可以設置這樣的邊框! 實現原理很簡單,自行編寫一個ShapeDrawable的資源文件!然后TextView將blackgroung 設置為這個drawable資源即可!

簡單說下shapeDrawable資源文件的幾個節(jié)點以及屬性:

  • 這個是設置背景顏色的
  • 這個是設置邊框的粗細,以及邊框顏色的
  • 這個是設置邊距的
  • 這個是設置圓角的
  • 這個是設置漸變色的,可選屬性有: startColor:起始顏色 endColor:結束顏色 centerColor:中間顏色 angle:方向角度,等于0時,從左到右,然后逆時針方向轉,當angle = 90度時從下往上 type:設置漸變的類型

實現效果圖:

代碼實現:

Step 1:編寫矩形邊框的Drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 設置一個黑色邊框 -->
    <stroke android:width="2px" android:color="#000000"/>
    <!-- 漸變 -->
    <gradient
        android:angle="270"
        android:endColor="#C0C0C0"
        android:startColor="#FCD209" />
    <!-- 設置一下邊距,讓空間大一點 -->
    <padding
        android:left="5dp"
        android:top="5dp"
        android:right="5dp"
        android:bottom="5dp"/>

</shape> 

Step 2:編寫圓角矩形邊框的Drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 設置透明背景色 -->
    <solid android:color="#87CEEB" />

    <!-- 設置一個黑色邊框 -->
    <stroke
        android:width="2px"
        android:color="#000000" />
    <!-- 設置四個圓角的半徑 -->
    <corners
        android:bottomLeftRadius="10px"
        android:bottomRightRadius="10px"
        android:topLeftRadius="10px"
        android:topRightRadius="10px" />
    <!-- 設置一下邊距,讓空間大一點 -->
    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />

</shape>

Step 3:將TextView的blackground屬性設置成上面這兩個Drawable:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txtOne"
        android:layout_width="200dp"
        android:layout_height="64dp"
        android:textSize="18sp"
        android:gravity="center"
        android:background="@drawable/txt_rectborder"
        android:text="矩形邊框的TextView" />

    <TextView
        android:id="@+id/txtTwo"
        android:layout_width="200dp"
        android:layout_height="64dp"
        android:layout_marginTop="10dp"
        android:textSize="18sp"
        android:gravity="center"
        android:background="@drawable/txt_radiuborder"
        android:text="圓角邊框的TextView" />

</LinearLayout>

2.3 帶圖片(drawableXxx)的TextView:

在實際開發(fā)中,我們可能會遇到這種需求:

如圖,要實現這種效果,可能你的想法是:一個ImageView用于顯示圖片 + 一個TextView用于顯示文字,然后把他們丟到一個LinearLayout中,接著依次創(chuàng)建四個這樣的小布局,再另外放到一個大的LinearLayout中,效果是可以實現,但是會不會有點繁瑣呢?而且前面我們前面也說過,布局層次越少,性能越好!使用drawableXxx就可以省掉上面的過程,直接設置四個TextView就可以完成我們的需求!

基本用法:

設置圖片的核心其實就是:drawableXxx;可以設置四個方向的圖片:drawableTop(上),drawableButtom(下),drawableLeft(左),drawableRight(右) 另外,你也可以使用drawablePadding來設置圖片與文字間的間距!

效果圖:(設置四個方向上的圖片)

實現代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context="com.jay.example.test.MainActivity" >  

    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_centerInParent="true"  
        android:drawableTop="@drawable/show1"  
        android:drawableLeft="@drawable/show1"  
        android:drawableRight="@drawable/show1"  
        android:drawableBottom="@drawable/show1"  
        android:drawablePadding="10dp"  
        android:text="張全蛋" />  

</RelativeLayout> 

一些問題: 可能你會發(fā)現,我們這樣設置的drawable并不能自行設置大小,在XML是無法直接設置的; 所以我們需要在Java代碼中來進行一個修改!

示例代碼如下:

package com.jay.example.test;  

import android.app.Activity;  
import android.graphics.drawable.Drawable;  
import android.os.Bundle;  
import android.widget.TextView;  

public class MainActivity extends Activity {  
    private TextView txtZQD;  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        txtZQD = (TextView) findViewById(R.id.txtZQD);  
        Drawable[] drawable = txtZQD.getCompoundDrawables();  
        // 數組下表0~3,依次是:左上右下  
        drawable[1].setBounds(100, 0, 200, 200);  
        txtZQD.setCompoundDrawables(drawable[0], drawable[1], drawable[2],  
                drawable[3]);  
    }  
} 

運行效果圖:

代碼分析:

  • ①Drawable[] drawable = txtZQD.getCompoundDrawables( ); 獲得四個不同方向上的圖片資源,數組元素依次是:左上右下的圖片
  • ②drawable[1].setBounds(100, 0, 200, 200); 接著獲得資源后,可以調用setBounds設置左上右下坐標點,比如這里設置了代表的是: 長是:從離文字最左邊開始100dp處到200dp處 寬是:從文字上方0dp處往上延伸200dp!
  • ③txtZQD.setCompoundDrawables(drawable[0], drawable[1], drawable[2], drawable[3]);為TextView重新設置drawable數組!沒有圖片可以用null代替哦! PS:另外,從上面看出我們也可以直接在Java代碼中調用setCompoundDrawables為 TextView設置圖片!

2.4 使用autoLink屬性識別鏈接類型

當文字中出現了URL,E-Mail,電話號碼,地圖的時候,我們可以通過設置autoLink屬性;當我們點擊 文字中對應部分的文字,即可跳轉至某默認APP,比如一串號碼,點擊后跳轉至撥號界面!

看下效果圖:

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號