以编程方式更改现有形状中的纯色

时间:2016-10-25 12:34:49

标签: java android android-imageview android-drawable

我有一个带有自定义边框的圆形视图,使用CircularImageView库实现并在RecyclerView中充气。到目前为止,我已将图像放入其中,但现在我想将其替换为单色圆形和文本。

我创建了以下形状(shape_colored_circle.xml):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <size
        android:height="70dp"
        android:width="70dp"/>
    <solid
        android:color="#00f"/>
</shape>

并在TextView中将CircularImageViewFrameLayout一起添加:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <com.mikhaellopez.circularimageview.CircularImageView
            android:id="@+id/card_thumbnail"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/icon"
            app:civ_border="true"
            app:civ_border_color="@color/border_color"
            app:civ_border_width="2dp"
            app:civ_shadow="true"
            app:civ_shadow_color="@color/grey"
            app:civ_shadow_radius="10"
            android:contentDescription="@string/card_thumbnail"/>

        <TextView
            android:id="@+id/card_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/app_name"
            android:textSize="18sp"
            android:textStyle="bold"/>

    </FrameLayout>

</LinearLayout>

我知道布局看起来有点奇怪(&#34;为什么顶部LinearLayout是必要的?&#34;),但如果没有它,视图就不会出现在{ {1}}而这个布局(取自图书馆的例子)满足了我们的需求。

RecyclerView中,我将形状设置为onBindViewHolder资源:

CircularImageView

最后我看到了我的期待。问题是我的drawable提前有一个特定的颜色。因此,我尝试以编程方式创建一个形状:

holder.thumbnail.setImageDrawable(activity.getResources().getDrawable(R.drawable.shape_colored_circle));

但视图只是变成了一个黑色矩形。我尝试了一些以编程方式创建形状的解决方案(所有这些都非常相似)无济于事 - 我要么看到黑色矩形,要么根本没有看到。

我做错了吗?是否有另一种方法来创建一个形状,或者某种方式在运行时更改现有形状?或者可能采用完全不同的方式来实现所需的行为?

谢谢!

2 个答案:

答案 0 :(得分:3)

[[u' 2015', u'$60.65M '], [u' 2014', u'$100M '], [u' 2013', u'$36.55M '], [u' 2012', u'$4M ']]

答案 1 :(得分:1)

感谢Vijay Pal Vishwakarma我设法解决了这个问题。

首先,我将预定义的形状设置为CircularImageView

的来源
<com.mikhaellopez.circularimageview.CircularImageView
            android:id="@+id/card_thumbnail"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/shape_colored_circle"      //Note this line
            app:civ_border="true"
            app:civ_border_color="@color/border_color"
            app:civ_border_width="2dp"
            app:civ_shadow="true"
            app:civ_shadow_color="@color/grey"
            app:civ_shadow_radius="10"
            android:contentDescription="@string/card_followee_large_thumbnail"/>

然后,在onBindViewHolder中,我得到了现有的drawable并改变了它的颜色:

GradientDrawable gd = (GradientDrawable) holder.thumbnail.getDrawable();
if (gd != null) {
    gd.setColor(colors[position]);
}