Android按钮叠加边框的自定义视图或矢量可绘制
创始人
2024-10-06 07:02:08
0

要实现Android按钮叠加边框的自定义视图或矢量可绘制,可以使用以下步骤:

  1. 创建一个自定义视图类,继承自Button类。
public class BorderButton extends Button {

    public BorderButton(Context context) {
        super(context);
        init();
    }

    public BorderButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BorderButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        // 设置按钮的背景为空
        setBackground(null);
        // 设置按钮的文字颜色
        setTextColor(getResources().getColor(android.R.color.black));
        // 设置按钮的边框宽度和颜色
        setStrokeWidth(2);
        setStrokeColor(getResources().getColor(R.color.border_color));
    }

    private void setStrokeWidth(int width) {
        GradientDrawable drawable = (GradientDrawable) getBackground();
        if (drawable != null) {
            drawable.setStroke(width, drawable.getStrokeColor());
        }
    }

    private void setStrokeColor(int color) {
        GradientDrawable drawable = (GradientDrawable) getBackground();
        if (drawable != null) {
            drawable.setStroke(drawable.getStrokeWidth(), color);
        }
    }
}
  1. 在res/drawable文件夹中创建一个矢量可绘制的XML文件,用于定义按钮的背景。


    
    
    

  1. 在res/values/colors.xml文件中定义边框的颜色。


    #FF0000

  1. 在布局文件中使用自定义的按钮视图。

这样,你就可以创建一个带有叠加边框的自定义按钮视图。

相关内容

热门资讯

透视辅助!aapoker透视怎... 透视辅助!aapoker透视怎么用(透视)德普之星透视辅助,教程方针(确实有挂)-哔哩哔哩1、每一步...
透视有挂!pokerrrr2辅... 透视有挂!pokerrrr2辅助(透视)hhpoker一直输有挂吗,教程法子(果真有挂)-哔哩哔哩h...
透视关于!hhpoker脚本下... 透视关于!hhpoker脚本下载(透视)wepoker怎么买辅助,教程方式(有挂方式)-哔哩哔哩1、...
透视透视!wepoker透视脚... 您好,wepoker亲友圈有用吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【48527505...
透视开挂!wepoker透视脚... 透视开挂!wepoker透视脚本是什么(透视)wepoker辅助器,教程方案(真的有挂)-哔哩哔哩1...
透视曝光!pokemmo免费脚... 透视曝光!pokemmo免费脚本(透视)wepoker辅助是真的假的,教程模块(有挂助手)-哔哩哔哩...
透视推荐!hhpoker脚本(... 透视推荐!hhpoker脚本(透视)wepoker辅助是真的吗,教程技法(有挂秘笈)-哔哩哔哩;小薇...
透视解谜!wpk透视工作室(透... 透视解谜!wpk透视工作室(透视)德州局透视脚本下载安装最新版本,教程秘籍(有挂秘笈)-哔哩哔哩1、...
透视科普!hh poker插件... 透视科普!hh poker插件下载(透视)安装不了wepoker,教程大纲(有挂方略)-哔哩哔哩1、...
透视曝光!wepoker私人辅... 透视曝光!wepoker私人辅助器(透视)哈糖大菠萝攻略,教程烘培(有挂讲解)-哔哩哔哩1、下载好w...