【问题标题】:Change drawable xml color from widget从小部件更改可绘制的 xml 颜色
【发布时间】:2021-08-13 03:01:43
【问题描述】:

我想更改 widget 中可绘制 xml 的颜色。需要考虑的事情是drawable xml有一个圆形半径,并且drawable需要设置为相对/线性布局,以便将内容包裹起来。

我已尝试将可绘制对象设置为图像视图并通过setColorFilter 设置颜色。这对我不起作用,因为我需要将它包装到布局中

使用setBackgroundColor 更改相对布局的颜色将替换不留圆角半径的可绘制对象。

颜色是动态改变的,所以不能在drawable中设置它或使用多个drawable

有什么办法可以做到吗?

【问题讨论】:

    标签: java android kotlin


    【解决方案1】:

    xml

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/imgBackground"
        android:layout_width="@dimen/_36sdp"
        android:layout_height="@dimen/_36sdp"
        android:layout_margin="@dimen/_4sdp"
        android:background="@drawable/rounded_custom_color"
        android:padding="@dimen/_6sdp">
    
    </RelativeLayout>
    

    在你的 java 类中

    ((GradientDrawable) imgBackground.getBackground()).setColor(listColors.get(position));
    

    这里的“listColors”是

    ArrayList<Integer> listColors=getColorList();
    
    public ArrayList<Integer> getColorList() {
        ArrayList<Integer> list=new ArrayList<>();
        int[] bg_colors = getResources().getIntArray(R.array.bg_color);
       
         for (int i = 0; i < bg_colors.length; i++) {
             list.add(bg_colors[i]);
         }
    
     return list;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多