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. 在布局文件中使用自定义的按钮视图。

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

相关内容

热门资讯

实测必看!wepoker可以免... 实测必看!wepoker可以免费玩吗,wepoker的辅助器,黑科技教程(有挂透明)是一款可以让一直...
透视能赢!大菠萝789辅助器下... 透视能赢!大菠萝789辅助器下载(透视)都是真的是有挂(详细辅助wpk教程)透视能赢!大菠萝789辅...
新手必备!hhpoker德州挂... 新手必备!hhpoker德州挂真的有吗,wepoker免费透视,辅助教程(有挂透明);实战中需综合运...
透视透视!wepoker有脚本... 透视透视!wepoker有脚本吗(透视)好像真的是有挂(详细辅助系统教程)软件透明挂微扑克wpk插件...
必看攻略!aapoker脚本,... 必看攻略!aapoker脚本,aapoker怎么提高中牌率,专业教程(有挂透视);支持2-10人实时...
透视脚本!aapoker辅助软... 透视脚本!aapoker辅助软件合法吗(透视)都是存在有挂(详细辅助科技教程)1、系统规律教程、辅助...
必备科技!wepoker有机器... 必备科技!wepoker有机器人吗,wepoker私人局透视,靠谱教程(有挂软件)科技教程也叫必备教...
透视脚本!德普辅助器辅助器怎么... 透视脚本!德普辅助器辅助器怎么用(透视)确实是真的有挂(详细辅助扑克教程)在进入辅助挂后,参与本局比...
教学盘点!wepoker轻量版... 1、教学盘点!wepoker轻量版辅助,wepoker透视挂底牌,攻略教程(有挂技巧)。2、wepo...
透视了解!pokemmo修改器... 透视了解!pokemmo修改器手机版(透视)确实真的是有挂(详细辅助规律教程)1、让任何用户在无需A...