TextView组件
1. 文本的长宽属性
android:layout_width
android:layout_height
按住Ctril鼠标点击layout_width或者layout_height可以查看可以赋哪些值。
<enum name="fill_parent" value="-1" />
<enum name="match_parent" value="-1" />
<enum name="wrap_content" value="-2" />
每个变量的作用如下表所示:
name | 作用 |
---|---|
fill_parent | 此常量被弃用,并且被match_parent替换 |
match_parent | 与父视图一样大 |
wrap_content | 取决于文本的大小 |
除此之外,还可以赋值具体的数字并且需要携带单位名称(px/Resolution/Dpi/Density/Dip/dp):
名称 | 解释 |
---|---|
px(像素) | 图像元素,是作为图像构成的基本单元,单个像素并不固定,跟随屏幕的大小和像素数量的关系变化 |
Resolution | 指定屏幕的垂直与水平方向的像素数量,如果分辨率是1920*1080,那就是垂直方向1920个像素,水平方向1080像素 |
Dpi(像素密度) | 是指屏幕每英寸距离中有多少个像素点 |
Density(密度) | 是指屏幕上每平方英寸中含有像素点的数量 |
Dip/dp(设备独立像素) | 也可以叫dp,长度单位,同一个单位在不同设备上有不同的显示效果,具体效果根据设备的密度有关系 |
2. 设置文本id
相当于TextView的名字,格式为:
android:id="@+id/ + 名称"
例如:
android:id="@+id/tv_1"
作用是,可以在java代码中获取该TextView,同时可以使用java代码对该属性进行修改。
3. 设置文本text
1.java方法
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv_1 = findViewById(R.id.tv_1);
tv_1.setText("qianjiu");
}
}
运行结果如下图所示:
2.xml方法
android:id="@+id/tv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="qianjiu1">
运行代码结果如下:
当然,这种写法不属于规范写法,应引入string.xml文件的字符串,例如:
android:text="@string/app_name">
运行结果如下图所示:
此外,当xml与java文件都对该组件写入文本时,以java文件为主。
4.文本颜色textcolour
颜色属性为32位的二进制组成,八位一组,从左至右依次是透明度+红+绿+蓝。
1.xml方法
android:textColor="#FFFF00FF"
运行结果如下:
正确的使用方法是调用应引入colour.xml文件的字符串。例如:
android:textColor="@color/black"
2.java方法
TextView tv_1 = findViewById(R.id.tv_1);
tv_1.setTextColor(R.color.black);
5.文本风格
名称 | 作用 |
---|---|
normal | 无效果 |
bold | 加粗 |
italic | 斜体 |
例如:
android:textStyle="bold"
运行结果如下:
6.字体大小
单位一般为sp。
1.xml方法
android:textSize="20sp"
2.java方法
tv_1.setTextSize(30);
运行结果如下图所示:
7.背景颜色
android:background="@color/green"
运行结果如下图所示:
8.设置控件对齐方式
android:gravity="对齐方式"
其中,对齐方式有一下几种:
名称 | 对齐方式 |
---|---|
top | 上对齐 |
bottom | 下对齐 |
left | 左对齐 |
right | 右对齐 |
center_vertical | 垂直中心 |
fill_vertical | 增大对象的垂直大小,使其完全填满容器 |
center_horizontal | 水平中心 |
fill_horizontal | 增大对象的水平大小,使其完全填满容器 |
center | 在垂直轴和水平轴上将对象放置在其容器的中心 |
fill | 增加对象的水平和垂直大小,使其完全填满容器 |
clip_vertical | 如果需要,可以增大对象的水平和垂直大小,使其完全填充containerAdditional选项,该选项可以设置为具有对象的上边缘和/或下边缘 |
clip_horizontal | 可以设置为将子对象的左边缘和/或右边缘剪裁到其容器边界的附加选项 |
start | 将对象推到其容器的开头,不改变其大小 |
end | 将对象推到其容器的结尾,不改变其大小 |
为了方便查看运行效果,我将TextView的长和宽增大
android:layout_width="600px"
android:layout_height="400px"
例如:
android:gravity="center"
运行代码如下:
带阴影的TextView组件
总代码:
android:shadowColor="@color/black"
android:shadowRadius="3.0"
android:shadowDx="10.0"
android:shadowDy="10.0"
1.阴影的颜色
值得注意的是,单独设置阴影颜色是没有效果的。
android:shadowColor="@color/black"
2.阴影的模糊程度
设置0.1不明显,建议设置3.0,可以直观的看出效果。
android:shadowRadius="3.0"
当android:shadowRadius = 0.1时
当android:shadowRadius = 3.0时
或者更大当android:shadowRadius = 30.0时,(这里仅仅作为效果的对比)。
3.阴影的水平偏移
android:shadowDx="10.0"
运行效果如下图所示:
4.阴影的垂直偏移
android:shadowDy="10.0"
运行效果如下图所示:
实现跑马灯的TextView
1.内容单行显示
android:singleLine="true"
2.省略方式
android:ellipsize="marquee"
3.流动的次数
永久流动
android:marqueeRepeatLimit="marquee_forever"
4.获取焦点
android:focusable="true"
android:focusableInTouchMode="true"
此时,跑马灯并没有跑起来,有以下几种解决方法:
1.获取点击
android:clickable="true"
2.获取焦点
创建一个新的TextView
继承TextView属性。
右键点击MyTextView。
选择前三个。
修改为自己的模板。
运行结果如下图所示:
3.请求焦点
在TextView组件内请求焦点。
总体代码
xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_1"
android:textColor="@color/red"
android:layout_width="match_parent"
android:layout_height="400px"
android:text="@string/app_name"
android:textStyle="bold"
android:textSize="30sp"
android:background="@color/green"
android:gravity="center"
android:shadowColor="@color/black"
android:shadowRadius="3.0"
android:shadowDx="10.0"
android:shadowDy="10.0"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true">
<requestFocus/>
</TextView>
</LinearLayout>
工程代码文件下载地址:https://download.csdn.net/download/DHKSHFJ/85163844