TextView文本框

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

使用TextView控件

使用TextView只需我們在xml布局中加入<TextView/> 標簽稍计,其中寬高屬性是必須的,正常也會有text屬性裕循、id屬性等臣嚣。以下示例代碼展示了一個TextView的簡單使用:

  • android:id :設置控件的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的形式設置)

  • 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>

效果預覽圖:

TextView

上面的示例中TextView是一個寬高自適應內(nèi)容,位置居中的控件剥哑。如果需要動態(tài)修改或設置文本內(nèi)容或樣式硅则,可以使用setText方法、setTextColor方法等株婴。

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

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

  • setTextColor(int color) :動態(tài)設置修改文本顏色蘸际,該方法有多個重寫方法,可傳入不同參數(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);//通過id找到控件
        textView.setText("Hello");
        textView.setTextColor(getResources().getColor(R.color.purple_700,this.getTheme()));
        textView.setTextSize(30);
    }
}

效果預覽圖:

動態(tài)修改TextView

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

TextView背景邊框

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

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

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

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 <!-- 實心 -->
 <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>

再設置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>

設置完了之后效果就有了:

drawable背景邊框

實現(xiàn)漸變背景也特別容易褥伴,只需要修改drawable即可谅将,另建一個新的drawable資源文件實現(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對齊方式

當TextView有寬度和高度時饥臂,可以設置文本的對齊方式,同樣在布局容器里似踱,TextView控件自身也可以設置相對于容器的對齊方式隅熙。

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

  • android:layout_gravity :控件的對齊方式

以下示例展示了TextView對齊方式的使用:

<?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>

效果預覽圖:

對齊方式

咋一看上面兩個TextView對齊方式一模一樣,其實效果都是水平居中核芽,只是TextView1是文本居中囚戚,寬度填充父容器;TextView2是控件居中轧简,寬度自適應內(nèi)容驰坊,這也是gravitylayout_gravity的區(qū)別。

TextView其它屬性

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

設置drawableXX屬性的TextView帶一個或多個圖像恶导,位置和屬性后面的XX方向有關浆竭,比如下面的示例:

<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" />

效果預覽圖:

drawableXX屬性

本文簡單的講解了一些TextView的屬性和方法,但是知道這些還遠遠不夠,更多的屬性方法還得查官方文檔TextView邦泄。官方文檔就像字典一樣删窒,知識權威全面,現(xiàn)在的技術更新迭代如此頻繁顺囊,Android開發(fā)者網(wǎng)站也是學習Android最好的網(wǎng)站肌索。

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市特碳,隨后出現(xiàn)的幾起案子诚亚,更是在濱河造成了極大的恐慌,老刑警劉巖午乓,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件站宗,死亡現(xiàn)場離奇詭異,居然都是意外死亡益愈,警方通過查閱死者的電腦和手機梢灭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蒸其,“玉大人敏释,你說我怎么就攤上這事∶” “怎么了钥顽?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長靠汁。 經(jīng)常有香客問我蜂大,道長,這世上最難降的妖魔是什么膀曾? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任县爬,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己遭贸,他們只是感情好,可當我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布耳高。 她就那樣靜靜地躺著,像睡著了一般所踊。 火紅的嫁衣襯著肌膚如雪泌枪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天秕岛,我揣著相機與錄音碌燕,去河邊找鬼误证。 笑死,一個胖子當著我的面吹牛修壕,可吹牛的內(nèi)容都是我干的愈捅。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼慈鸠,長吁一口氣:“原來是場噩夢啊……” “哼蓝谨!你這毒婦竟也來了?” 一聲冷哼從身側響起青团,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤譬巫,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后督笆,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體芦昔,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年娃肿,在試婚紗的時候發(fā)現(xiàn)自己被綠了烟零。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡咸作,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出宵睦,到底是詐尸還是另有隱情记罚,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布壳嚎,位于F島的核電站桐智,受9級特大地震影響,放射性物質發(fā)生泄漏烟馅。R本人自食惡果不足惜说庭,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望郑趁。 院中可真熱鬧刊驴,春花似錦、人聲如沸寡润。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽梭纹。三九已至躲惰,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間变抽,已是汗流浹背础拨。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工氮块, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人诡宗。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓滔蝉,卻偏偏與公主長得像,于是被迫代替她去往敵國和親僚焦。 傳聞我的和親對象是個殘疾皇子锰提,可洞房花燭夜當晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容