今天主要介绍的UI组件为:
TextView
会演示TextView怎样将文本带上边框
与超文本链接
TextView
自定义带边框的TextView
1.activity文件
package cn.class3g.activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyBorderTextView extends TextView{
//必须实现带两个参数的构造
public MyBorderTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
//覆盖父类的onDraw方法
public void onDraw(Canvas canvas){
super.onDraw(canvas);
//创建画刷
Paint paint = new Paint();
//设置颜色
paint.setColor(android.graphics.Color.GREEN);
//开画
canvas.drawLine(0, 0, this.getWidth()-1, 0, paint);//左边框
canvas.drawLine(0, 0,0,this.getHeight()-1, paint);//上边框
canvas.drawLine(this.getWidth()-1, 0, this.getWidth()-1, this.getHeight()-1, paint);//右边框
canvas.drawLine(0,this.getHeight()-1, this.getWidth()-1, this.getHeight()-1, paint);//下边框
//canvas.drawLine(开始横坐标,开始纵坐标,结束横坐标,结束纵坐标)
}
}
2.layout文件
<cn.class3g.activity.MyBorderTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="30dp"
android:layout_margin="10dp"
android:textColor="#cccccc"
android:text="一雨落尘"
/>
Layout文件中定义了内外边距,文本内容,颜色
显示效果:
超文本链接
1、Activity文件
package cn.class3g.activity;
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
public class TextViewTestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.auto_link);
//Html.fromHtml方法
TextView tv = (TextView) this.findViewById(R.id.tvHtml);
String htmlStr = "<font color='#00FF22'>我爱北京天安门</font>" +//改变字体颜色的一种方法
"<a href='http://www.ifeng.com'>小桥流水</a>";//具有超链接外观,但是不能跳转
tv.setText(Html.fromHtml(htmlStr));
}
}
注:Html.fromHtml()方法的查连接徒具其形,但不能跳转
2、auto_link.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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:text="@string/webUrl" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="email"
android:text="@string/email" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="phone"
android:text="@string/phoneNumber" />
<TextView
android:id="@+id/tvHtml"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
这个文件简单的说明了超链接(auto_link)的使用方法,每种链接都有自己的格式,如果格式错误,不能链接
3、strings.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, TextViewTestActivity!</string>
<string name="app_name">TextViewTest</string>
<string name="webUrl">凤凰网:http://www.ifeng.com</string>
<string name="email">小强的邮箱:1234abc@163.com</string>
<string name="phoneNumber">电话号码:1383838438</string>
</resources>
模拟器显示效果: