android开发之自定义带边框的TextView

本文介绍如何在Android中自定义一个带边框的TextView,并通过XML布局文件实现其应用。
 

自定义带边框的TextView

///////////////////////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);

          

      }

      @Override

      protected void onDraw(Canvas canvas) {

           super.onDraw(canvas);

          

           Paint paint = new Paint();

          

           paint.setColor(android.graphics.Color.YELLOW);

          

 

           canvas.drawLine(0, 0, this.getWidth()-1, 0, paint);

//1、横坐标0到this.getWidth()-1,纵坐标0到0

           canvas.drawLine(0, 0, 0, this.getHeight()-1, paint);

//2、横坐标0到0,纵坐标0到this.getHeight()-1

           canvas.drawLine(this.getWidth()-1, 0, this.getWidth()-1, this.getHeight()-1, paint);

//3、横坐标this.getWidth()-1到this.getWidth()-1,纵坐标0到this.getHeight()-1

           canvas.drawLine(0, this.getHeight()-1, this.getWidth()-1, this.getHeight()-1, paint);

//4、横坐标0到this.getWidth()-1,纵坐标this.getHeight()-1到this.getHeight()-1

//下面用图介绍边框的绘制

      }

 

}

1

this.getWidth,this.getHeight

0,this.getHeight

this.getWidth()-1,0

0,0

边框的绘制

3

4

2

 

 

 


然后只需要在布局里调用这个就行

<?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" >

 

    <cn.class3g.activity.MyBorderTextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_margin="10dp"

        android:padding="30dp"

        android:text="hello"

        android:textColor="#cccccc" >

    </cn.class3g.activity.MyBorderTextView>

 

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_margin="10dp"

        android:padding="30dp"

        android:text="hello hello hello hello"

        android:background="@drawable/ic_launcher"

        android:textColor="#cccccc" >

    </TextView>

 

</LinearLayout>

效果图:

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值