TextView文本框

TextView是Android的文本框,用于向用戶(hù)顯示文本的UI元素,是Android中使用頻率最高的控件之一。TextView繼承于View,所以View中的屬性和方法大多數(shù)也都適用于TextView。

使用TextView控件

使用TextView只需我們?cè)趚ml布局中加入<TextView/> 標(biāo)簽,其中寬高屬性是必須的,正常也會(huì)有text屬性、id屬性等。以下示例代碼展示了一個(gè)TextView的簡(jiǎn)單使用:

  • android:id :設(shè)置控件的id 用于java代碼中找到該控件

  • android:layout_width : 控件的寬度 常用值 match_parent(填充父容器) wrap_content(包裹內(nèi)容)

  • android:layout_height : 控件的高度 常用值 match_parent(填充父容器) wrap_content(包裹內(nèi)容)

  • android:text : 文本內(nèi)容(建議使用@string/xxx的形式設(shè)置)

  • android:textColor android:textSize android:textStyle: 字體顏色 字體大小 字體樣式

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity">
 <TextView
 android:id="@+id/textView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="this is TextView"
 android:textColor="@color/black"
 android:textSize="18sp"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

效果預(yù)覽圖:

TextView

上面的示例中TextView是一個(gè)寬高自適應(yīng)內(nèi)容,位置居中的控件。如果需要?jiǎng)討B(tài)修改或設(shè)置文本內(nèi)容或樣式,可以使用setText方法、setTextColor方法等。

  • setText(CharSequence Text) : 動(dòng)態(tài)設(shè)置修改文本內(nèi)容,該方法有多個(gè)重寫(xiě)方法,可傳入不同參數(shù)

  • setTextSize(float size) :動(dòng)態(tài)設(shè)置修改文本大小,該方法有多個(gè)重寫(xiě)方法,可傳入不同參數(shù)

  • setTextColor(int color) :動(dòng)態(tài)設(shè)置修改文本顏色,該方法有多個(gè)重寫(xiě)方法,可傳入不同參數(shù)

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = (TextView) findViewById(R.id.textView);//通過(guò)id找到控件
        textView.setText("Hello");
        textView.setTextColor(getResources().getColor(R.color.purple_700,this.getTheme()));
        textView.setTextSize(30);
    }
}

效果預(yù)覽圖:

動(dòng)態(tài)修改TextView

除了動(dòng)態(tài)設(shè)置TextView的內(nèi)容,我們還可以動(dòng)態(tài)獲取TextView的內(nèi)容,獲取內(nèi)容需要用到getText方法,同理也可以獲取TextView的樣式或其它信息,更多方法可以參考官網(wǎng)文檔。

TextView背景邊框

TextView可以自定義背景,背景可以設(shè)置為背景顏色和圖像等,利用drawable資源還可以設(shè)置背景邊框和漸變背景等。

  • android:background : 背景填充(使用背景顏色或drawable資源)

以下示例展示了一個(gè)背景邊框,先創(chuàng)建一個(gè)drawable資源文件,將文件放入res目錄下的drawable文件夾,代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 <!-- 實(shí)心 -->
 <solid android:color="#FFFFFF"/>
 <!-- 邊框 -->
 <stroke
 android:width="1dp"
 android:color="#FF0000"/>
 <!-- 圓角 -->
 <corners android:radius="3dp"/>
 <!-- 邊距 -->
 <padding
 android:top="2dp"
 android:bottom="2dp"
 android:left="6dp"
 android:right="6dp"/>
</shape>

再設(shè)置background屬性為drawable資源:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity">
 <TextView
 android:id="@+id/textView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="this is TextView"
 android:textColor="@color/teal_700"
 android:textSize="18sp"
 android:background="@drawable/background"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

設(shè)置完了之后效果就有了:

drawable背景邊框

實(shí)現(xiàn)漸變背景也特別容易,只需要修改drawable即可,另建一個(gè)新的drawable資源文件實(shí)現(xiàn)漸變背景,代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 <gradient
 android:startColor="#FF0000"
 android:endColor="#FF00FF"
 android:angle="360"
 android:centerX="0.5"
 android:centerY="0.5"/>
 <corners
 android:radius="3dp"/>
 <padding
 android:top="2dp"
 android:bottom="2dp"
 android:left="6dp"
 android:right="6dp"/>
</shape>

再將background屬性值替換為漸變背景的drawable,效果就變成了下面的:

drawable漸變背景

TextView對(duì)齊方式

當(dāng)TextView有寬度和高度時(shí),可以設(shè)置文本的對(duì)齊方式,同樣在布局容器里,TextView控件自身也可以設(shè)置相對(duì)于容器的對(duì)齊方式。

  • android:gravity : 字體或內(nèi)容的對(duì)齊方式

  • android:layout_gravity :控件的對(duì)齊方式

以下示例展示了TextView對(duì)齊方式的使用:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity">
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintHorizontal_bias="0.0"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toTopOf="parent"
 app:layout_constraintVertical_bias="0.0">
 <TextView
 android:id="@+id/textView"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:text="this is TextView1"
 android:textColor="@color/teal_700"
 android:textSize="18sp" />
 <TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:text="this is TextView2"
 android:textColor="@color/teal_200"
 android:textSize="18sp" />
 </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

效果預(yù)覽圖:

對(duì)齊方式

咋一看上面兩個(gè)TextView對(duì)齊方式一模一樣,其實(shí)效果都是水平居中,只是TextView1是文本居中,寬度填充父容器;TextView2是控件居中,寬度自適應(yīng)內(nèi)容,這也是gravitylayout_gravity的區(qū)別。

TextView其它屬性

TextView屬性很多,每個(gè)屬性都演示不太現(xiàn)實(shí)。除了以上的屬性,還經(jīng)常使用到marginXX(外邊距)、paddingXX(內(nèi)邊距)、drawableXX(帶drawable圖像)等屬性。

設(shè)置drawableXX屬性的TextView帶一個(gè)或多個(gè)圖像,位置和屬性后面的XX方向有關(guān),比如下面的示例:

<TextView
 android:id="@+id/textView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:drawableBottom="@mipmap/ic_launcher"
 android:text="TextView"
 android:textColor="@color/purple_700"
 android:textSize="18sp" />

效果預(yù)覽圖:

drawableXX屬性

本文簡(jiǎn)單的講解了一些TextView的屬性和方法,但是知道這些還遠(yuǎn)遠(yuǎn)不夠,更多的屬性方法還得查官方文檔TextView。官方文檔就像字典一樣,知識(shí)權(quán)威全面,現(xiàn)在的技術(shù)更新迭代如此頻繁,Android開(kāi)發(fā)者網(wǎng)站也是學(xué)習(xí)Android最好的網(wǎng)站。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容