问题描述
我有一个改造参数,它只是颜色,其数据形式为“#0DEAB5”,我需要在我的回收站视图中设置它并显示颜色列表,为此我创建了一个 imageView 并分配我对 XML 的 imagView 的参数,但在回收器视图中未查看颜色..所以任何可能有帮助的解决方案请分享...
解决方法
1.create circle Drawable
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>
<solid
android:color="@color/colorPrimary"
/>
<size
android:width="30dp"
android:height="30dp"/>
</shape>
2.set drawable to view
<View
android:layout_width="@dimen/_40sdp"
android:background="@drawable/dummy"
android:layout_height="@dimen/_40sdp"/>
3.onbind in reclyeradapter
Drawable mDrawable = ContextCompat.getDrawable(context,R.drawable.circle);
mDrawable.setColorFilter(new PorterDuffColorFilter(Color.parseColor("#000000"),PorterDuff.Mode.MULTIPLY));
final int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
yourview.setBackgroundDrawable(mDrawable);
} else {
yourview.setBackground(mDrawable);
}`
Here we don't want to use glide for transform the view to circle and change the alpha in Color.parseColor("#000000")
,
试试这个:
String color = "#FF" + yourColorString.replace("#",""); // if you get a color of only 6 digits and need an alpha
String color = "#0DEAB5"; // if you don't care about the alpha
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor(color));
yourImageView.setImageDrawable(colorDrawable);
还要注意你的ImageView必须有一个设置的大小,因为colorDrawable没有。