背景+带边框(圆角)的textview怎么设置
发布日期:2021-06-30 21:08:05 浏览次数:2 分类:技术文章

本文共 1741 字,大约阅读时间需要 5 分钟。

有三种方式可以实现

  • 方法一:

带有透明图片的背景图,这个没有什么好将的,自己制作一个边框图就行 ,然后设置background就可以了

为要添加边框的TextView添加一个background

android:background="@drawable/textview_border" 

  • 方法二:

通过shape来设置背景图片

首先创建一个 textview_border.xml 文件放在drawable文件夹里面

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

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >  

   <solid android:color="#ffffff" />  

   <stroke android:width="1dip" android:color="#4fa5d5"/>  

</shape>

然后设置成背景   android:background="@drawable/textview_border" 

  • 方法三:

编写一个继承TextView类的自定义组件,并在onDraw事件方法中画边框。

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);

    }

}

第二种方法常用,一些案例如下:

//如果当做是LevelListDrawable使用时值为true,否则为false.
//右下角的圆角半径
//使用LevelListDrawable时就要设置为true。设为false时才有渐变效果
//虚线的间隔

圆角矩形,扫描式渐变:

圆形,线性渐变

虚线

环形,放射型渐变

结合使用,在 selector 中用shape背景

 

 

 

转载地址:https://liuyan.blog.csdn.net/article/details/81488518 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:第二技能
下一篇:textview设置独特字颜色和背景颜色

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月21日 04时49分13秒