android 动态创建selector状态选择器 动态创建Drawable

最近在做一个使用接口返回的字符串:"#ff0000" 来动态设置drawable背景颜色与动态设置状态选择器selector的需求,之前写习惯了shape的xml,还是第一次写动态的,有点搞笑,搞笑的是自己没写过,不知道会不会写。

1.很简单,直接布局代码:

XML 复制代码
<RelativeLayout
        android:id="@+id/ll_title"
        android:layout_width="match_parent"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:layout_height="50dp"
        app:contentInsetStart="0dp"
        android:background="@drawable/shape_red_bg"
        tools:ignore="MissingConstraints">

        <TextView
            android:id="@+id/toolbar_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:singleLine="true"
            android:text="菜单"
            android:textColor="@color/white"
            android:textSize="16dp"
            android:visibility="visible" />

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:singleLine="true"
            android:text="居中标题"
            android:layout_centerInParent="true"
            android:textColor="@android:color/white"
            android:textSize="20dp"
            android:visibility="visible" />

        <TextView
            android:id="@+id/toolbar_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginRight="10dp"
            android:gravity="center"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="秘密"
            android:textColor="@color/white"
            android:textSize="16dp"
            android:visibility="visible" />
    </RelativeLayout>
XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <!--    <size android:height="417dp" android:width="834dp"/>-->

    <gradient
        android:gradientRadius="417dp"
        android:startColor="@color/red"
        android:endColor="@color/red"
        android:centerX="0.5"
        android:centerY="1"
        android:type="radial" />

    <corners
        android:bottomLeftRadius="0dp"
        android:bottomRightRadius="0dp"
        android:topLeftRadius="0dp"
        android:topRightRadius="0dp" />
</shape>

2.Activity代码如下:

java 复制代码
//在Activity的初始化中直接复制下面代码:
RelativeLayout llTitle = findViewById(R.id.ll_title);
llTitle.setBackgroundDrawable(getStateListDrawable());
        llTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });


//在Activity中的方法
public static StateListDrawable getStateListDrawable() {
        StateListDrawable selector = new StateListDrawable();
        int parseColor = Color.parseColor("#FF00FF");
        Drawable pressDrawable = getRoundRectDrawable(20, Color.parseColor("#ff00ff"), true, 0);
        Drawable selectDrawable = getRoundRectDrawable(20, Color.parseColor("#00ffff"), true, 0);
        Drawable defaultDrawable = getRoundRectDrawable(20, Color.parseColor("#00ffff"), true, 0);

        selector.addState(new int[]{android.R.attr.state_pressed}, pressDrawable);
        selector.addState(new int[]{android.R.attr.state_selected}, selectDrawable);
        // 添加一个默认状态, 默认状态必须写在其他状态的最后面, 否则其他状态失效
        selector.addState(new int[]{}, defaultDrawable);

        return selector;
    }

    public static GradientDrawable getRoundRectDrawable(int radius, int color, boolean isFill, int strokeWidth){
        //左上、右上、右下、左下的圆角半径
        float[] radiusList = {radius, radius, radius, radius, radius, radius, radius, radius};
        GradientDrawable drawable = new GradientDrawable();
        drawable.setCornerRadii(radiusList);
        drawable.setColor(isFill ? color : Color.TRANSPARENT);
        drawable.setStroke(isFill ? 0 : strokeWidth, color);
        return drawable;
    }
java 复制代码
//补充设置渐变色
public static GradientDrawable getRoundRectDrawable(int radius, int color, boolean isFill, int strokeWidth){
        //左上、右上、右下、左下的圆角半径
        float[] radiusList = {radius, radius, radius, radius, radius, radius, radius, radius};
        GradientDrawable drawable = new GradientDrawable();
        //设置渐变色,渐变方向从左上到右下,渐变颜色设置
        GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TL_BR,
                new int[]{R.color.blue_0B1152, R.color.blue_4E5988});
        drawable.setCornerRadii(radiusList);
        drawable.setColor(isFill ? color : Color.TRANSPARENT);
        drawable.setStroke(isFill ? 0 : strokeWidth, color);
        return drawable;
    }

亲测有效,写这篇文章只是想记录一下完整的实现代码。

相关博客推荐:

Android状态选择器selector的详细使用汇总

selector的两种设置方式

相关推荐
abigale031 小时前
webpack+vite前端构建工具 -11实战中的配置技巧
前端·webpack·node.js
专注API从业者1 小时前
构建淘宝评论监控系统:API 接口开发与实时数据采集教程
大数据·前端·数据库·oracle
Joker`s smile1 小时前
Chrome安装老版本、不同版本,自制便携版本用于前端调试
前端·chrome
weixin_416639971 小时前
爬虫工程师Chrome开发者工具简单介绍
前端·chrome·爬虫
我是如子啊1 小时前
【解决“此扩展可能损坏”】Edge浏览器(chrome系列通杀))扩展损坏?一招保留数据快速修复
前端·chrome·edge
灵性花火1 小时前
Qt的前端和后端过于耦合(0/7)
开发语言·前端·qt
孤水寒月5 小时前
基于HTML的悬窗可拖动记事本
前端·css·html
祝余呀5 小时前
html初学者第一天
前端·html
脑袋大大的6 小时前
JavaScript 性能优化实战:减少 DOM 操作引发的重排与重绘
开发语言·javascript·性能优化
速易达网络8 小时前
RuoYi、Vue CLI 和 uni-app 结合构建跨端全家桶方案
javascript·vue.js·低代码