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

自定义带边框的TextView

///Activity///

01 package cn.class3g.activity;
02  
03 import android.content.Context;
04 import android.graphics.Canvas;
05 import android.graphics.Paint;
06 import android.util.AttributeSet;
07 import android.widget.TextView;
08  
09  
10 public class MyBorderTextView extends TextView{
11  
12       public MyBorderTextView(Context context, AttributeSet attrs) {
13            super(context, attrs);       
14       }
15  
16       @Override
17       protected void onDraw(Canvas canvas) {
18            super.onDraw(canvas);
19            
20            Paint paint = new Paint();          
21            paint.setColor(android.graphics.Color.YELLOW);          
22  
23            canvas.drawLine(00this.getWidth()-10, paint);
24  
25 //1、横坐标0到this.getWidth()-1,纵坐标0到0
26  
27            canvas.drawLine(000this.getHeight()-1, paint);
28  
29 //2、横坐标0到0,纵坐标0到this.getHeight()-1
30  
31            canvas.drawLine(this.getWidth()-10this.getWidth()-1this.getHeight()-1, paint);
32  
33 //3、横坐标this.getWidth()-1到this.getWidth()-1,纵坐标0到this.getHeight()-1
34  
35            canvas.drawLine(0this.getHeight()-1this.getWidth()-1this.getHeight()-1, paint);
36  
37 //4、横坐标0到this.getWidth()-1,纵坐标this.getHeight()-1到this.getHeight()-1
38  
39 //下面用图介绍边框的绘制
40  
41       }
42  
43 }

1

this.getWidth,this.getHeight

0,this.getHeight

this.getWidth()-1,0

0,0

边框的绘制

3

4

2


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

01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03     android:layout_width="match_parent"
04     android:layout_height="match_parent"
05     android:orientation="vertical" >
06  
07     <cn.class3g.activity.MyBorderTextView
08         android:layout_width="wrap_content"
09         android:layout_height="wrap_content"
10         android:layout_margin="10dp"
11         android:padding="30dp"
12         android:text="hello"
13         android:textColor="#cccccc" >
14     </cn.class3g.activity.MyBorderTextView>
15   
16  
17     <TextView
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         android:layout_margin="10dp"
21         android:padding="30dp"
22         android:text="hello hello hello hello"
23         android:background="@drawable/ic_launcher"
24         android:textColor="#cccccc" >
25     </TextView>
26  
27  
28 </LinearLayout>

效果图:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值