Android 控件 - TextView
1、TextView
https://www.bilibili.com/video/BV13y4y1E7pF?p=3
1.1、layout_width、layout_height
match_parent:和容易保持一樣的寬高
wrap_content:根據控件自動分配寬高
數值:比如 200dp 寫固定大小
1.2、id
給控件設置id,方便其他代碼獲取這個控件
<TextViewandroid:id="@+id/tv_one"android:layout_width="200dp"android:layout_height="200dp" />獲取id為 tv_one的 TextView
TextView tv_one = findViewById(R.id.tv_one); tv_one.setText("blake");1.3、text
給TextView設置內容,但是如果java代碼修改了這個內容,則java代碼修改的優先
<TextViewandroid:id="@+id/tv_one"android:text="test01"android:layout_width="200dp"android:layout_height="200dp" /> TextView tv_one = findViewById(R.id.tv_one); tv_one.setText("blake");1.4、textColor
設置顏色
<TextViewandroid:id="@+id/tv_one"android:text="test01"android:textColor="#F60606"android:layout_width="200dp"android:layout_height="200dp" />1.5、textStyle
設置字體風格:
normal 無效果
bold 加粗
italic 斜體
1.6、textSize
字體大小,單位一般用sp
<TextViewandroid:id="@+id/tv_one"android:layout_width="200dp"android:layout_height="200dp"android:text="test01"android:textColor="#F60606"android:textSize="30sp"android:textStyle="bold" />1.7、background
控件背景顏色,可以理解為填充整個控件的顏色,可以是圖片
<TextViewandroid:id="@+id/tv_one"android:layout_width="200dp"android:layout_height="200dp"android:text="test01"android:textColor="#F60606"android:textSize="30sp"android:background="#DFA4A4"android:textStyle="bold" />1.7、gravity
設置控件中內容的對齊方向
有top、bottom、left、right、center_vertical、center_horizontal…
可以按住ctrl 然后鼠標點擊 gravity 查看更多支持
2、TextView 的text 和 顏色 的正規寫法
在實際開發中不會把 直接 寫 android:text=“test01”、android:textColor="#F60606"、android:background="#DFA4A4"
會在
color.xml和strings.xml中配置好了直接引用
color.xml
我只添加了red,其他都是默認的
strings.xml
我只添加了tv_one
引用
<TextViewandroid:id="@+id/tv_one"android:layout_width="200dp"android:layout_height="200dp"android:text="@string/tv_one"android:textColor="@color/red"android:textSize="30sp"android:background="@color/black"android:gravity="center"android:textStyle="bold" />3、帶陰影的TextView
https://www.bilibili.com/video/BV13y4y1E7pF?p=4
- android:shadowColor: 設置陰影顏色,需要與shadowRadius一起使用
- android:shadowRadius:設置陰影的模糊程度,設置為0.1就變成了字體顏色了,建議使用3.0
- android:shadowDx:設置陰影在水平方向的偏移,就是水平方向陰影開始的橫坐標位置
- android:shadowDy:設置陰影在豎直方向的偏移,就是豎直方向陰影開始的縱坐標位置
4、實現跑馬燈效果的TextView
https://www.bilibili.com/video/BV13y4y1E7pF?p=5
就是循環展示一行 string
- android:singleLine:內容單行顯示
- android:focusable:是否可以獲取焦點
- android:focusableInTouchMode:用于控制視圖在觸摸模式下是否可以聚焦
- android:ellipsize:在哪里省略文本
- android:marqueeRepeatLimit:字幕動畫重復的次數
默認是不會跑起來的,有三種方法可以讓他跑起來(我用的模擬器自動就能跑起來。。。)
1、android:clickable:可以點擊
設置android:clickable=“true”,點擊一下能開始跑
2、自定義一個TextView
讓 isFocused 返回 true
package com.example.demo01;import android.content.Context; import android.util.AttributeSet; import android.widget.TextView;import androidx.annotation.Nullable;public class MyTextView extends TextView {public MyTextView(Context context) {super(context);}public MyTextView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);}public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overridepublic boolean isFocused() {return true;} } <com.example.demo01.MyTextViewandroid:id="@+id/tv_one"android:layout_width="match_parent"android:layout_height="200dp"android:gravity="center"android:shadowColor="@color/red"android:shadowDx="10.0"android:shadowDy="10.0"android:shadowRadius="3.0"android:singleLine="true"android:focusable="true"android:focusableInTouchMode="true"android:ellipsize="marquee"android:marqueeRepeatLimit="marquee_forever"android:text="@string/tv_one"android:textColor="@color/black"android:textSize="30sp"android:textStyle="bold" />3、加一個 requestFocus
<TextViewandroid:id="@+id/tv_one"android:layout_width="match_parent"android:layout_height="200dp"android:gravity="center"android:shadowColor="@color/red"android:shadowDx="10.0"android:shadowDy="10.0"android:shadowRadius="3.0"android:singleLine="true"android:focusable="true"android:focusableInTouchMode="true"android:ellipsize="marquee"android:marqueeRepeatLimit="marquee_forever"android:text="@string/tv_one"android:textColor="@color/black"android:textSize="30sp"android:textStyle="bold"><requestFocus/></TextView>https://www.bilibili.com/video/BV13y4y1E7pF?p=3
https://www.bilibili.com/video/BV13y4y1E7pF?p=4
https://www.bilibili.com/video/BV13y4y1E7pF?p=5
總結
以上是生活随笔為你收集整理的Android 控件 - TextView的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用C++实现十进制转二进制【个人思想】
- 下一篇: html-下拉框、文本域、文件域