2017-08-22 3 views
0
<ImageView 
    android:id="@+id/imageView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:backgroundTint="@color/colorRed" 
    android:background="@drawable/ic_delete"/> 

上記のように、ImageViewバックグラウンド用のアンドロイドVectorAssetを追加しました。
そして、以下のように、Vector Assetの色を赤色から青色に変更することができます。プログラムで背景のベクトルアセットを使用してImageViewのbackgroundTintを変更する

android:backgroundTint="@color/colorBlue" 

ですが、プログラム的に色を変更したいと思います。
私はさまざまな方法を試みたが、そのどれもがすべてのバックグラウンドとSRCの最初の

答えて

0

の代わりにあなたがAppCompatImageViewを使用することができますImageViewを使用して、使用してContext

のインスタンスである

setBackgroundTintList(contextInstance.getResources().getColorStateList(R.color.your_xml_name)); 

を使用して、それをロードはAPIレベル21からサポートされます。AppCompatImageViewを使用する場合は、setSupportBackgroundTintListを使用して色合いを変更できます。

ので

あなたはこのようImageViewの変更、あなたがこのような色合いの色を設定するsetSupportBackgroundTintListを呼び出すことができるように

<android.support.v7.widget.AppCompatImageView 
    android:id="@+id/imageView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:backgroundTint="@color/colorRed" 
    android:background="@drawable/ic_delete"/> 

imageView.setSupportBackgroundTintList(ContextCompat.getColorStateList(this, R.color.colorBlue)); 
0

を働いていないがImageViewのために異なる特性があります。 イメージをImageViewに設定しようとしていますか?あなたがプロパティを使用する必要がありますベクトル資産を使用している場合 android:src="drawable"

:まずあなたがプロパティを使用する必要があり、すべてのWhat is the difference between background, backgroundTint, backgroundTintMode attributes in android layout xml?

app:srcCompat="drawable"

背景を理解するために、backgroundTintプロパティは、こののリードを持っています

、それはあなたが本当に欲しいものである場合は、最後にプログラム的背景の色合いにあなたの質問に答えるために、そして、ここでHow to add button tint programmatically

はそれが役に立てば幸いリンクです!

0

This質問は関連していますが、ボタンの回答です。

user A.A


によってオリジナルの答えあなたは色状態リストリソースの作成方法を知っているこのlinkをフォローsetBackgroundTintList(ColorStateList list)

を使用する必要があります。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item 
     android:color="#your_color_here" /> 
</selector> 

その後、0123ためcontextInstanceがAPPCOMPAT

btnTag.setSupportButtonTintList(ContextCompat.getColorStateList(Activity.this, R.color.colorPrimary)); 
関連する問題