自定义带边框的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( 0 , 0 , this .getWidth()- 1 , 0 , paint); |
24 |
25 | //1、横坐标0到this.getWidth()-1,纵坐标0到0 |
26 |
27 | canvas.drawLine( 0 , 0 , 0 , this .getHeight()- 1 , paint); |
28 |
29 | //2、横坐标0到0,纵坐标0到this.getHeight()-1 |
30 |
31 | canvas.drawLine( this .getWidth()- 1 , 0 , this .getWidth()- 1 , this .getHeight()- 1 , paint); |
32 |
33 | //3、横坐标this.getWidth()-1到this.getWidth()-1,纵坐标0到this.getHeight()-1 |
34 |
35 | canvas.drawLine( 0 , this .getHeight()- 1 , this .getWidth()- 1 , this .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 > |
效果图: