美文网首页
为TextView添加一个边框的几种办法

为TextView添加一个边框的几种办法

作者: sun_wenming | 来源:发表于2017-04-05 12:02 被阅读0次

方法一:

比较土 ,加背景图片,透明的带边框的背景图片
设置到android:background就可以

方法二:创建一个 shape 设置到android:background就可以

 <?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#00000000"/>
    <stroke android:width="2dip" android:color="#ff000000" />
</shape>

方法三:(太麻烦)

编写一个继承TextView类的自定义组件,并在onDraw事件方法中画边框。
然后 在布局文件 中 使用 自定义 textview 即可

public class BorderTextView extends TextView{  
  
    public BorderTextView(Context context) {  
        super(context);  
    }  
    public BorderTextView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
    }  
    private int sroke_width = 1;  
    @Override  
    protected void onDraw(Canvas canvas) {  
        Paint paint = new Paint();  
        //  将边框设为黑色  
        paint.setColor(android.graphics.Color.BLACK);  
        //  画TextView的4个边  
        canvas.drawLine(0, 0, this.getWidth() - sroke_width, 0, paint);  
        canvas.drawLine(0, 0, 0, this.getHeight() - sroke_width, paint);  
        canvas.drawLine(this.getWidth() - sroke_width, 0, this.getWidth() - sroke_width, this.getHeight() - sroke_width, paint);  
        canvas.drawLine(0, this.getHeight() - sroke_width, this.getWidth() - sroke_width, this.getHeight() - sroke_width, paint);  
        super.onDraw(canvas);  
    }  
}  

相关文章

网友评论

      本文标题:为TextView添加一个边框的几种办法

      本文链接:https://www.haomeiwen.com/subject/iybtattx.html