2017-06-13 67 views
1

我有在圖像之間有一些透明區域的png圖像。我試圖設置顏色以編程方式到那個區域,但它不工作。如何在Android中設置圖像的背景顏色

這是我的XML佈局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="vertical"> 

<com.mikhaellopez.circularimageview.CircularImageView 
    android:id="@+id/iv_cat_icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:src="@drawable/del" 
    card_view:civ_border_color="@color/tabsScrollColor" /> 
    </RelativeLayout> 

在XML中使用該card_view在XML我能夠設置顏色我已經自定義屬性

xmlns:card_view="http://schemas.android.com/apk/res-auto 

,但我需要以編程方式更改其color.is無論如何,我可以設置此屬性編程?請任何人幫助我。 how to fill color

+0

的'檢查對象CircularImageView'它具有功能來設置顏色。 –

+0

我嘗試,但它不工作。 – sunil

回答

2

CircularImageView是一個自定義視圖。您可以更改它的屬性,如下所示。

CircularImageView circularImageView = (CircularImageView)findViewById(R.id.iv_cat_icon); 
    // Set Border 
    circularImageView.setBorderColor(getResources().getColor(R.color.tabsScrollColor)); 
    circularImageView.setBorderWidth(10); 
+0

謝謝,它的工作。 – sunil

0
CircularImageView imageView = (CircularImageView)findViewById(R.id.iv_cat_icon); 

Drawable color = new ColorDrawable(getResources().getColor(R.color.your_color)); 
Drawable image = getResources().getDrawable(R.drawable.your_drawable); 

LayerDrawable ld = new LayerDrawable(new Drawable[]{color, image}); 
imageView.setImageDrawable(ld); 
相關問題