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

自定义带边框的TextView
///Activity///
package cn.class3g.activity;
  1. import android.content.Context;
  2. import android.graphics.Canvas;
  3. import android.graphics.Paint;
  4. import android.util.AttributeSet;
  5. import android.widget.TextView;

  6. public class MyBorderTextView extends TextView{

  7.       public MyBorderTextView(Context context, AttributeSet attrs) {
  8.            super(context, attrs);
  9.          
  10.       }
  11.       @Override
  12.       protected void onDraw(Canvas canvas) {
  13.            super.onDraw(canvas);
  14.          
  15.            Paint paint = new Paint();
  16.          
  17.            paint.setColor(android.graphics.Color.YELLOW);
  18.          

  19.            canvas.drawLine(0, 0, this.getWidth()-1, 0, paint);
  20. //1、横坐标0到this.getWidth()-1,纵坐标0到0
  21.            canvas.drawLine(0, 0, 0, this.getHeight()-1, paint);
  22. //2、横坐标0到0,纵坐标0到this.getHeight()-1
  23.            canvas.drawLine(this.getWidth()-1, 0, this.getWidth()-1, this.getHeight()-1, paint);
  24. //3、横坐标this.getWidth()-1到this.getWidth()-1,纵坐标0到this.getHeight()-1
  25.            canvas.drawLine(0, this.getHeight()-1, this.getWidth()-1, this.getHeight()-1, paint);
  26. //4、横坐标0到this.getWidth()-1,纵坐标this.getHeight()-1到this.getHeight()-1
  27. //下面用图介绍边框的绘制
  28.       }

  29. }
复制代码
1
this.getWidth,this.getHeight
0,this.getHeight
this.getWidth()-1,0
0,0
边框的绘制

3
4
2
然后只需要在布局里调用这个就行
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical" >

  6.     <cn.class3g.activity.MyBorderTextView
  7.         android:layout_width="wrap_content"
  8.         android:layout_height="wrap_content"
  9.         android:layout_margin="10dp"
  10.         android:padding="30dp"
  11.         android:text="hello"
  12.         android:textColor="#cccccc" >
  13.     </cn.class3g.activity.MyBorderTextView>

  14.     <TextView
  15.         android:layout_width="wrap_content"
  16.         android:layout_height="wrap_content"
  17.         android:layout_margin="10dp"
  18.         android:padding="30dp"
  19.         android:text="hello hello hello hello"
  20.         android:background="@drawable/ic_launcher"
  21.         android:textColor="#cccccc" >
  22.     </TextView>

  23. </LinearLayout>
复制代码
效果图:

8.gif (6.52 KB, 下载次数: 2)

下载附件  保存到相册

2011-12-26 22:35 上传


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值