设为首页 加入收藏

TOP

Android开发之自定义带边框的TextView
2014-11-24 14:14:34 来源: 作者: 【 】 浏览:0
Tags:Android 开发 定义 边框 TextView

自定义带边框的TextView


///////////////////////Activity///////////////////////////////


package cn.class3g.activity;



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


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


}



}








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


< xml version="1.0" encoding="utf-8" >


"http://schemas.android.com/apk/res/android"


android:layout_width="match_parent"


android:layout_height="match_parent"


android:orientation="vertical" >




android:layout_width="wrap_content"


android:layout_height="wrap_content"


android:layout_margin="10dp"


android:padding="30dp"


android:text="hello"


android:textColor="#cccccc" >





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





效果图:



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android开发之EditText组件 下一篇Android开发之Android的菜单

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: