在适配器中设置 GradientDrawable 颜色
Set GradientDrawable Color in Adapter
我从按钮背景中获取颜色并将其作为字符串存储在数据库中。后来我想在我的 recyclerView 适配器中使用这个颜色字符串来设置我的 TextView 的颜色。下面是我的代码:
@Override
public void onBindViewHolder(NoteListAdapter.NoteListHolder holder, int position) {
current = data.get(position);
final String text = current.getText();
final String get_tag_text = current.getTag();
final String get_tag_color = current.getTag_color();
int[] colors = {Color.parseColor(get_tag_color)};
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
holder.note_text.setText(text);
holder.tv_tag_text.setBackground(gd);
holder.tv_tag_text.setText(get_tag_text);
}
我得到的错误是“未知颜色”。数据库中保存的颜色格式为(The saved color format is android.graphics.drawable.GradientDrawable@d1790a4
)
下面是从按钮背景可绘制文件和我的按钮 xml 代码
获取颜色的代码
color = (GradientDrawable) tag_watchlist.getBackground().mutate();
tag_color= color.toString();
<Button
android:id="@+id/tag_watch"
style="@style/tag_buttons"
android:background="@drawable/watchlist_button"
android:text="Watchlist" />
按钮背景的可绘制文件代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:padding="10dp">
<solid android:color="#a40ce1"/>
<corners android:radius="10dp"/>
</shape>
谁能告诉我如何解决这个问题?
您需要为 GradientDrawable
、startColor
和 endColor
提供至少两种颜色
它可能会抛出一个异常 java.lang.IllegalArgumentException: needs >= 2 number of colors
这段代码:
int[] colors = {Color.parseColor(get_tag_color)};
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
用这个更改您的代码:
int[] colors = {Color.parseColor(start_color), Color.parseColor(end_color)};
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
如果您的 startColor
和 endColor
都有 get_tag_color
,则相应地进行替换,但这对 GradientDrawable
没有帮助。
Edited answer
您遇到异常 Caused by: java.lang.IllegalArgumentException: Unknown color
意味着您没有将颜色以支持的格式传递给方法 Color.parseColor
。
确保按以下格式传递值
#RRGGBB
#AARRGGBB
这是有效的例子
Color.parseColor("#FF4081")
有关详细信息,请参阅文档 Color.parseColor
根据您的要求,您可以达到 API 24 级以上。如果您使用的是当前 minSdkVersion 24,请尝试以下
更改模型 class 以将颜色保存为 Integer
而不是 String
。
GradientDrawable gradientDrawable = (GradientDrawable) tag_watchlist.getBackground().mutate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
int color = gradientDrawable.getColor().getDefaultColor();
Log.d("TAG","Color is :"+color);
current.setTagColor(color); // where current is your model class
}
从模型中取回颜色
int color = current.getTagColor();
我从按钮背景中获取颜色并将其作为字符串存储在数据库中。后来我想在我的 recyclerView 适配器中使用这个颜色字符串来设置我的 TextView 的颜色。下面是我的代码:
@Override
public void onBindViewHolder(NoteListAdapter.NoteListHolder holder, int position) {
current = data.get(position);
final String text = current.getText();
final String get_tag_text = current.getTag();
final String get_tag_color = current.getTag_color();
int[] colors = {Color.parseColor(get_tag_color)};
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
holder.note_text.setText(text);
holder.tv_tag_text.setBackground(gd);
holder.tv_tag_text.setText(get_tag_text);
}
我得到的错误是“未知颜色”。数据库中保存的颜色格式为(The saved color format is android.graphics.drawable.GradientDrawable@d1790a4
)
下面是从按钮背景可绘制文件和我的按钮 xml 代码
获取颜色的代码 color = (GradientDrawable) tag_watchlist.getBackground().mutate();
tag_color= color.toString();
<Button
android:id="@+id/tag_watch"
style="@style/tag_buttons"
android:background="@drawable/watchlist_button"
android:text="Watchlist" />
按钮背景的可绘制文件代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:padding="10dp">
<solid android:color="#a40ce1"/>
<corners android:radius="10dp"/>
</shape>
谁能告诉我如何解决这个问题?
您需要为 GradientDrawable
、startColor
和 endColor
它可能会抛出一个异常 java.lang.IllegalArgumentException: needs >= 2 number of colors
这段代码:
int[] colors = {Color.parseColor(get_tag_color)};
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
用这个更改您的代码:
int[] colors = {Color.parseColor(start_color), Color.parseColor(end_color)};
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
如果您的 startColor
和 endColor
都有 get_tag_color
,则相应地进行替换,但这对 GradientDrawable
没有帮助。
Edited answer
您遇到异常 Caused by: java.lang.IllegalArgumentException: Unknown color
意味着您没有将颜色以支持的格式传递给方法 Color.parseColor
。
确保按以下格式传递值
#RRGGBB
#AARRGGBB
这是有效的例子
Color.parseColor("#FF4081")
有关详细信息,请参阅文档 Color.parseColor
根据您的要求,您可以达到 API 24 级以上。如果您使用的是当前 minSdkVersion 24,请尝试以下
更改模型 class 以将颜色保存为 Integer
而不是 String
。
GradientDrawable gradientDrawable = (GradientDrawable) tag_watchlist.getBackground().mutate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
int color = gradientDrawable.getColor().getDefaultColor();
Log.d("TAG","Color is :"+color);
current.setTagColor(color); // where current is your model class
}
从模型中取回颜色
int color = current.getTagColor();