Android用户界面之TextView
TextView是文本控件,TextView是个非常常用并且重要的控件,新建一个android工程,在xml布局文件中,就有一个TextView控件声明:
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />通常在Activity类的onCreate方法中初始化TextView,并设置相关属性,通常TextView通常有这些属性
| 方法 | xml属性 |
| setText | android:text |
| setTextSize | android:textSize |
| setTextColor | android:textColor |
| setBackgroundResource | android:background |
请看代码:
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//add you code here
textView=(TextView) findViewById(R.id.tv);
textView.setText("reset text");
textView.setTextColor(Color.GREEN);
textView.setTextSize(30);
textView.setBackgroundResource(android.R.drawable.edit_text); //拿系统资源的edit_text当背景
}将以上的代码移植到你的demo中,运行起来,就已经揭开textview一半的面纱了
第二部分:
为TextView设置链接(Email,URL,电话等)
android为TextView提供了4种链接实现方法:
1.将显示内容写在资源文件上,一般是strings.xml文件,并且用<a>来声明链接,而且要使用setMovementMethod()方法设置TextView可点击.
2.使用autoLink方法
3.使用Html类的fromHtml方法
4.使用spannable方法
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Demo</string>
<string name="action_settings">Settings</string>
<string name="baidu"><a href="http://www.baidu.com">百度一下,你就知道</a></string>
<string name="autoLink">http://www.baidu.com</string>
</resources>
activity_main.xml 部分
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/baidu" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv"
android:text="@string/autoLink" />
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv2"
android:text="@string/autoLink" />
<TextView
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv3"
android:text="@string/autoLink" />
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//add you code here
textView=(TextView) findViewById(R.id.tv);
tv2=(TextView) findViewById(R.id.tv2);
tv3=(TextView) findViewById(R.id.tv3);
tv4=(TextView) findViewById(R.id.tv4);
//textView.setText("reset text");
textView.setTextColor(Color.BLACK);
textView.setTextSize(30);
textView.setBackgroundResource(android.R.drawable.edit_text);
//显示内容包含在<a>中
textView.setMovementMethod(LinkMovementMethod.getInstance());
//使用autoLink
tv2.setAutoLinkMask(Linkify.ALL);
//Html类中的fromHtml方法格式化文本
tv3.setText(Html.fromHtml("<h5>this is some texts from html </h5><a href='http://www.baidu.com'>百度一下,你就知道</a>"));
SpannableString ss = new SpannableString("点击这里拨打电话,点击这里打开网页");
ss.setSpan(new URLSpan("tel:234567"), 0, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("http://www.baidu.com"), 9, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv4.setText(ss);
tv4.setMovementMethod(LinkMovementMethod.getInstance());
}可以发现,Demo中有四个文本,依次点击,看看有什么效果.
浙公网安备 33010602011771号