文本控件
安卓基础控制中的文本控件 TextView 的基本使用方法。
一、设置文本控件内容
1、XML 指定文本
在 activity_text.xml 文件中的 LinearLayout 节点内添加 TextView 节点, 通过 android:text 属性即可指定文本控件显示内容:
<TextView
android:id="@+id/tv_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello Word"/>
注意: 在实际开发中, XML 文件中的 text 属性中的值一般在常量 XML 中定义, 常量 XML 位于 res -> values -> strings.xml。
2、Java代码指定文本
在 TextActivity 类的 onCreate 方法中通过控件 ID 找到对应控件, 调用控件的 setText 方法写入文本信息:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text);
TextView textView = findViewById(R.id.tv_hello);
textView.setText("Hello Word");
}
注意: 同上, 对于静态常量应该在 strings.xml 文件中定义, 然后通过通过属性方法调用。
strings.xml 定义:
<resources>
<string name="app_name">study</string>
<string name="text2">Activity Study</string>
<string name="to_study">跳转</string>
<string name="hello">Hello Word</string>
</resources>
XML 写法:
<TextView
android:id="@+id/tv_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"/>
Java 写法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text);
TextView textView = findViewById(R.id.tv_hello);
textView.setText(getString(R.string.hello));
}
二、设置文本大小
1、XML 指定文本
在 activity_text.xml 文件中的 LinearLayout 节点内添加 TextView 节点, 通过 android:textSize 属性即可指定文本控件显示大小:
<TextView
android:id="@+id/tv_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textSize="30sp"/>
注意: textSize 属性的设置需要指定单位, 其中有: px、dp、sp、pt、in、mm。
2、Java代码指定文本
在 TextActivity 类的 onCreate 方法中通过控件 ID 找到对应控件, 调用控件的 setTextSize 方法写入文本大小:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text);
TextView textView = findViewById(R.id.tv_hello);
textView.setText(getString(R.string.hello));
textView.setTextSize(30);
//textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,30);
}
注意: setTextSize 默认单位为 TypedValue.COMPLEX_UNIT_SP。可选单位有:
public static final int COMPLEX_UNIT_PX = 0;
public static final int COMPLEX_UNIT_DIP = 1;
public static final int COMPLEX_UNIT_SP = 2;
public static final int COMPLEX_UNIT_PT = 3;
public static final int COMPLEX_UNIT_IN = 4;
public static final int COMPLEX_UNIT_MM = 5;
3、大小单位解释
对于安卓显示大小单位, 支持
- px: 像素, 是作为图像构成的基本单位, 单个像素的大小并不固定, 跟随屏幕大小和像素数量的关系变化。
- resolution: 分辨率, 是指屏幕的垂直和水平方向的像素数量, 如果分辨率是 1920*1080, 那就是垂直方向有 1920 个像素, 水平方向有 1080 个像素。
- dpi: 像素密度, 是指屏幕上每英寸(1英寸 = 2.54 厘米)距离中有多少个像素点。
- density: 密度, 这个是指屏幕上每平方英寸(2.54 ^ 2 平方厘米)中含有的像素点数量。
- dip / dp: 设备独立像素, 也可以叫做dp, 长度单位, 同一个单位在不同的设备上有不同的显示效果, 具体效果根据设备的密度有关。
三、设置文本颜色
1、XML 指定文本
在 activity_text.xml 文件中的 LinearLayout 节点内添加 TextView 节点, 通过 android:textColor 属性即可指定文本控件显示颜色:
<TextView
android:id="@+id/tv_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="#00FF00"/>
此处通过 # 标记, 直接写入颜色对应的编码即可。也可以在 res -> values -> colors.xml 文件中定义, 然后在 XML 或 Java 代码中直接引用即可。
colors.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="green">#FF00FF00</color>
</resources>
TextView 节点:
<TextView
android:id="@+id/tv_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="@color/green"/>
2、Java代码指定文本
在 TextActivity 类的 onCreate 方法中通过控件 ID 找到对应控件, 调用控件的 setTextColor 方法写入文本颜色:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text);
TextView textView = findViewById(R.id.tv_hello);
textView.setText(getString(R.string.hello));
textView.setTextColor(Color.GREEN);
}
此处可以用系统自带的 Color 类, 也可以直接写颜色对应的十六进制编码。
3、其他颜色设置
除却上述的字体颜色, 在 Java 代码中还可以通过 setBackgroundColor 或 setBackgroundResource 设置背景颜色; setHighlightColor 设置高亮文本颜色; setLinkTextColor 设置链接文本颜色; setHintTextColor 设置提示文本颜色。
//设置背景颜色
textView.setBackgroundColor(Color.GREEN);
textView.setBackgroundResource(R.color.green);
//设置高亮文本颜色
textView.setHighlightColor(Color.GREEN);
//设置提示文本颜色
textView.setHintTextColor(Color.GREEN);
//设置链接文本颜色
textView.setLinkTextColor(Color.GREEN);
同时, 也可以通过 XML 设置:
android:background="@color/green"
android:textColorHighlight="@color/green"
android:textColorHint="@color/green"
android:textColorLink="@color/green"