Android TextView文本控件介绍
作者:Chelovek 时间:2023-08-29 10:13:53
讲解
TextView作为Android最基础也是最常用的组件之一,他承担着文本的显示重任。要注意,其显示的文本内容是无法在界面直接被用户修改的。不过作为程序员,可以通过后台代码去修改TextView的内容和各项属性。另外要注意的是,TextView控件是需要放在容器里面的,比如LinearLayout容器(一般控件都要放在容器里面)。
基础属性介绍
属性 | 说明 |
---|---|
id | 设置一个组件id(唯一),通过findViewById()的方法获取到该对象,然后进行相关设置 |
layout_width | 设置组件宽度,可以填充数字和Android提供的枚举值,Android提供的两个枚举值:match_parent:与父类宽度匹配(减去padding)(在Level 8之后,代替废弃的fill_parent),wrap_content:组件应该足够大到足以其内容(加上padding,当然不超过其父类)。 |
layout_height | 设置组件高度,可以填充数字和Android提供的枚举值,Android提供的两个枚举值:match_parent:与父类高度匹配(减去padding)(在Level 8之后,代替废弃的fill_parent),wrap_content:组件应该足够大到足以其内容(加上padding,当然不超过其父类)。 |
text | 设置显示的文本内容 |
background | 设置背景颜色(或背景图片) |
textColor | 设置字体颜色 |
textStyle | 设置字体样式 ,三个可选值:normal(无效果),bold(加粗),italic(斜体) |
textSize | 字体大小,单位一般用sp |
gravity | 内容的对齐方向 |
示例:
<TextView
android:id="@+id/tView1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="修改"
android:textColor="@color/white"
android:textSize="90dp"
android:textStyle="bold"
android:gravity="center"
android:background="@color/black"/>
后台调用
Java后台通过ID调用。
注意:Java会覆盖对应的TextView 原本内容。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = findViewById(R.id.tView1);
tv.setText("TextView1");
}
来源:https://blog.csdn.net/weixin_42022380/article/details/122314308
标签:Android,TextView
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android开发之SQLite的使用方法
2022-11-12 05:26:38
![](https://img.aspxhome.com/file/2023/4/138074_0s.png)
Unity Shader实现玻璃材质效果
2021-10-27 05:25:29
![](https://img.aspxhome.com/file/2023/7/77227_0s.jpg)
详解java nio中的select和channel
2021-08-09 19:17:39
Android P实现静默安装的方法示例(官方Demo)
2022-04-05 20:06:13
Android中实现水平滑动(横向滑动)ListView示例
2021-09-07 18:15:03
MyBatis if test 判断字符串相等不生效问题
2021-10-11 22:42:01
IDEA设置maven修改settings.xml配置文件无法加载仓库的解决方案
2023-08-23 22:39:35
![](https://img.aspxhome.com/file/2023/5/66235_0s.png)
java中String的一些方法深入解析
2023-11-25 21:48:56
![](https://img.aspxhome.com/file/2023/2/60332_0s.png)
深入理解Java设计模式之状态模式
2022-07-07 12:49:44
![](https://img.aspxhome.com/file/2023/1/109941_0s.jpg)
解析Java中如何获取Spring中配置的bean
2023-07-20 13:35:26
java二维数组实现推箱子小游戏
2022-08-31 10:18:40
java多种幻灯片切换特效(经典)
2022-08-05 23:18:22
![](https://img.aspxhome.com/file/2023/5/95935_0s.jpg)
Android Fragment使用全解
2021-07-11 22:57:09
![](https://img.aspxhome.com/file/2023/9/114299_0s.png)
Flutter实现底部和顶部导航栏
2021-10-26 20:15:53
![](https://img.aspxhome.com/file/2023/5/130445_0s.jpg)
Android studio 3.5.2安装图文教程详解
2022-06-27 19:15:46
![](https://img.aspxhome.com/file/2023/5/90175_0s.jpg)
JAVAlogback日志管理详解
2023-01-11 22:33:54
![](https://img.aspxhome.com/file/2023/1/78861_0s.png)
Java用三元运算符判断奇数和偶数的简单实现
2023-07-24 16:54:03
![](https://img.aspxhome.com/file/2023/4/97944_0s.png)
Android观察者模式实例分析
2022-07-24 09:21:32
SpringBoot+Elasticsearch实现数据搜索的方法详解
2023-04-17 02:45:46
Spring boot 集成Dubbox的方法示例
2023-09-22 23:49:04