2016-09-03 2 views
1

を割り当てる方法を動的に我々はリングの描画可能を定義し、サンプルは以下のコピーとして、レイアウトxmlファイルでそれを使用し、リングの色

は、それがリングの描画可能を定義し、実行時に動的に色を変更することが可能であるだろうか?ユースケースはリストアイテムにあり、異なるアイテムのリングのリングカラーが異なる場合があります。

検索で誰かが何か提案があると良い解決策を見つけることができませんでした。

<ImageView 
        android:layout_width="10dp" 
        android:layout_height="10dp" 
        android:src="@drawable/ring" /> 

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
    android:top="4dp" 
    android:right="4dp" 
    android:bottom="4dp" 
    android:left="4dp"> 
    <shape 
     android:shape="oval"> 
     <solid android:color="#4d4d4d" /> 
    </shape> 
</item> 
<item> 
    <shape 
     android:shape="oval"> 
     <stroke android:width="2dp" 
      android:color="#4d4d4d"/> 
    </shape> 
</item> 

+0

細かい作業..私の答えをお試しください... –

答えて

1

まず...その

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:id="@+id/shape1" 
     android:top="4dp" 
     android:right="4dp" 
     android:bottom="4dp" 
     android:left="4dp"> 
     <shape 
      android:shape="oval"> 
      <solid android:color="#4d4d4d" /> 
     </shape> 
    </item> 
    <item 
     android:id="@+id/shape2" 

     > 
     <shape 

      android:shape="oval"> 
      <stroke android:width="2dp" 
       android:color="#4d4d4d"/> 
     </shape> 
    </item> 
    </layer-list> 

コードのようなことを描くあなたの層にIDを入れて

LayerDrawable shape = (LayerDrawable) ContextCompat.getDrawable(demo.this,R.drawable.ring); 
GradientDrawable gradientDrawable1 = (GradientDrawable) shape.findDrawableByLayerId(R.id.shape1); 

GradientDrawable gradientDrawable2 = (GradientDrawable) shape.findDrawableByLayerId(R.id.shape2); 

gradientDrawable1.setColor(ContextCompat.getColor(demo.this,R.color.color_first)); 

gradientDrawable2.setColor(ContextCompat.getColor(demo.this,R.color.color_second));// changing to black color 

ImageView imageView=(ImageView)findViewById(R.id.imageview); 
imageView.setBackground(shape); 

enter image description here

+0

おかげでアルジュン! <?xml version = "1.0" encoding = "utf-8"?> <固体 アンドロイド:色= "#1 ee8838" /> <ストローク アンドロイド:幅= "" 1DP アンドロイド:色= "#ff0000" /> lannyf

+0

今日私はで色の形を取って試してみて、idをitemに設定して、色を設定することができます..... –

+0

リングに2つの方法がありますこのメソッドで色の変更をしたい場合は、回答と一緒に変更されている、全体的な1つ2つの色sh ape.setStroke(strokeWidth、color)は、幅とintの色を上のように設定します。 –

関連する問題