Android之路-TextView组件

本文深入探讨Android中的TextView组件,展示如何为TextView添加边框,并讲解如何设置超文本链接,尽管Html.fromHtml()方法创建的链接无法实际跳转。通过activity文件、layout布局文件和strings资源文件的配置,实现自定义带边框的TextView以及显示超链接的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天主要介绍的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>


模拟器显示效果:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值