Android编程开发之TextView控件用法(2种方法)
作者:shichen2014 时间:2023-09-19 02:24:02
本文实例讲述了Android编程开发之TextView控件用法。分享给大家供大家参考,具体如下:
这里我们会讲讲常用控件的使用。
在今后的大多数章节里面也是一样的,我们会具体的说说某些控件的用法。因为只要把这些控件组合在一起它们就是一个应用了。
好吧我们直接看看这个控件怎么用。
细心的同学会发现,其实这个控件的内容是定义在values文件夹里面的strings.xml中的。
那么我们只需要给它加一段代码:
<string name="test">Welcome to Joven\'s blog</string>
然后绑定到控件上
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test" />
就可以了。我们来看一下效果。
其实还有第二种方法,也是最常用的方法,是动态获取的。
我们给TextView加个ID,在main.xml中这样写:
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test" />
然后再activity里面动态绑定,在TestDemo.java中这样写:
package com.example.testdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class TestDemo extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//绑定视图中id为tv1的控件textview。
tv=(TextView)findViewById(R.id.tv1);
//动态设置控件内容。
tv.setText("Welcome!");
}
}
好了我们再看看效果:
在优先级上面,程序是先加载资源文件再加载activity的。所以后者的设置会覆盖前者。
希望本文所述对大家Android程序设计有所帮助。
标签:Android,TextView
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
android ListView结合xutils3仿微信实现下拉加载更多
2023-11-29 11:11:50
Android Monkey压力测试详细介绍
2021-10-24 08:02:37
![](https://img.aspxhome.com/file/2023/0/137790_0s.png)
SpringMVC适配器模式作用范围介绍
2023-12-10 23:37:15
![](https://img.aspxhome.com/file/2023/2/122952_0s.png)
MyBatis-Plus逻辑删除和字段自动填充的实现
2023-01-08 03:45:54
![](https://img.aspxhome.com/file/2023/9/80289_0s.png)
C#设计模式实现之生成器模式和责任链模式
2023-12-19 21:57:51
![](https://img.aspxhome.com/file/2023/1/122641_0s.png)
Spring Cloud Eureka(全面解析) 大白话
2022-11-12 22:43:02
![](https://img.aspxhome.com/file/2023/5/62735_0s.png)
java和 javaw 及 javaws的区别解析
2023-07-21 06:31:05
![](https://img.aspxhome.com/file/2023/9/64749_0s.png)
Spring Boot缓存实战之Redis 设置有效时间和自动刷新缓存功能(时间支持在配置文件中配置)
2023-11-11 01:57:18
![](https://img.aspxhome.com/file/2023/3/59053_0s.png)
Unity之Luaframework框架lua调用C#方法
2022-02-14 11:22:26
![](https://img.aspxhome.com/file/2023/1/94631_0s.png)
C# PictureBox控件方法参数及图片删除重命名上传详解
2023-02-04 17:59:08
![](https://img.aspxhome.com/file/2023/1/106331_0s.png)
详解Dagger2在Android开发中的新用法
2021-08-23 22:39:40
Android中Retrofit 2.0直接使用JSON进行数据交互
2022-01-08 07:22:46
![](https://img.aspxhome.com/file/2023/4/138934_0s.png)
C#实现winform中RichTextBox在指定光标位置插入图片的方法
2022-04-04 01:50:16
利用枚举法求直方图中最大矩形面积的方法实例
2022-06-22 21:16:31
SpringBoot中Dozer的使用小结
2023-11-25 01:24:38
JAVA求两直线交点和三角形内外心的方法
2023-07-30 02:46:35
SSM Mapper文件查询出返回数据查不到个别字段的问题
2023-10-17 01:02:08
![](https://img.aspxhome.com/file/2023/3/58603_0s.png)
Android 点击生成二维码功能实现代码
2022-08-26 18:15:23
![](https://img.aspxhome.com/file/2023/8/137308_0s.png)
java实现仿windows 字体设置选项卡实例
2023-01-02 11:45:42
![](https://img.aspxhome.com/file/2023/0/92570_0s.png)
Unity工具类之生成文本验证码
2021-06-21 03:38:42