在运行时更改形状边框颜色

时间:2022-01-23 09:33:17

I have this shape in my drawable folder:

我在drawable文件夹中有这个形状:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="5dp" />
    <solid android:color="#ffffff" />
    <stroke android:width="2dp" android:color="#B5B5B5"/>
</shape>

This define a rectangle with rounded corners and I can apply it as background to any panel like this: android:background="@drawable/round_corner_shape".

这定义了一个带圆角的矩形,我可以将它作为背景应用于任何面板,如:android:background =“@ drawable / round_corner_shape”。

Here comes the question: I have few panels on my application, with the same shape as background, but for each shape I want a different border (stroke) color. I don't want to create 3 shapes, the only difference to be on the stroke color. Is it possible to change at runtime the stroke value?

问题就出现了:我的应用程序中有几个面板,形状与背景相同,但对于每个形状,我想要一个不同的边框(笔触)颜色。我不想创建3个形状,唯一的区别在于笔触颜色。是否可以在运行时更改行程值?

2 个解决方案

#1


17  

I had the same problem. In my case, I had a GridView, which the items in grid could have the border color changed by the user at runtime.

我有同样的问题。在我的例子中,我有一个GridView,网格中的项目可以在运行时由用户更改边框颜色。

So, in the gridviewAdapter for that grid, I did the following in the getView method (the one that generates the view for the adapter)

因此,在该网格的gridviewAdapter中,我在getView方法中执行了以下操作(生成适配器视图的方法)

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    convertView = inflater.inflate(R.layout.griditem, null);
    GradientDrawable gradientDrawable = (GradientDrawable) convertView.getBackground(); 

    gradientDrawable.setStroke(2, mColor); 
    convertView.invalidate();
    return convertView;
}

mColor is a int that represents the color, much like we do in the xml files. In java code, instead of "#" we use "0x" to define it in the AARRGGBB format. For example, use 0xFF000000 for 100% opaque BLACK and 0xFF0000FF for 100% opaque BLUE. Explaining this here since the google api 'helpfully' tells that the int color is "the color of the stroke".

mColor是一个表示颜色的int,就像我们在xml文件中一样。在java代码中,我们使用“0x”代替“#”来以AARRGGBB格式定义它。例如,对于100%不透明BLACK使用0xFF000000,对于100%不透明BLUE使用0xFF0000FF。在这里解释这个,因为谷歌api'有帮助'告诉int颜色是“中风的颜色”。

This solved my problem... I guess you can try something similar for your case.

这解决了我的问题...我想你可以尝试类似的情况。

#2


1  

Hi you can try create your background on runtime, then you can change it whenever you want.

您好,您可以尝试在运行时创建您的背景,然后您可以随时更改它。

RoundRectShape rect = new RoundRectShape(
  new float[] {30,30, 30,30, 30,30, 30,30},
  null,
  null);
ShapeDrawable bg = new ShapeDrawable(rect);
bg.getPaint().setColor(0x99FFFFFF);
view.setBackgroundDrawable(bg);

#1


17  

I had the same problem. In my case, I had a GridView, which the items in grid could have the border color changed by the user at runtime.

我有同样的问题。在我的例子中,我有一个GridView,网格中的项目可以在运行时由用户更改边框颜色。

So, in the gridviewAdapter for that grid, I did the following in the getView method (the one that generates the view for the adapter)

因此,在该网格的gridviewAdapter中,我在getView方法中执行了以下操作(生成适配器视图的方法)

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    convertView = inflater.inflate(R.layout.griditem, null);
    GradientDrawable gradientDrawable = (GradientDrawable) convertView.getBackground(); 

    gradientDrawable.setStroke(2, mColor); 
    convertView.invalidate();
    return convertView;
}

mColor is a int that represents the color, much like we do in the xml files. In java code, instead of "#" we use "0x" to define it in the AARRGGBB format. For example, use 0xFF000000 for 100% opaque BLACK and 0xFF0000FF for 100% opaque BLUE. Explaining this here since the google api 'helpfully' tells that the int color is "the color of the stroke".

mColor是一个表示颜色的int,就像我们在xml文件中一样。在java代码中,我们使用“0x”代替“#”来以AARRGGBB格式定义它。例如,对于100%不透明BLACK使用0xFF000000,对于100%不透明BLUE使用0xFF0000FF。在这里解释这个,因为谷歌api'有帮助'告诉int颜色是“中风的颜色”。

This solved my problem... I guess you can try something similar for your case.

这解决了我的问题...我想你可以尝试类似的情况。

#2


1  

Hi you can try create your background on runtime, then you can change it whenever you want.

您好,您可以尝试在运行时创建您的背景,然后您可以随时更改它。

RoundRectShape rect = new RoundRectShape(
  new float[] {30,30, 30,30, 30,30, 30,30},
  null,
  null);
ShapeDrawable bg = new ShapeDrawable(rect);
bg.getPaint().setColor(0x99FFFFFF);
view.setBackgroundDrawable(bg);