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

方法一:

比较土 ,加背景图片,透明的带边框的背景图片
设置到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);  
    }  
}  
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容