2017-01-26 9 views
0

不確定色を設定するための属性を持つ、アプリケーションの周りに使用されるカスタムプログレスバーがあります。私はサポートライブラリを使用しているので、古いアンドロイドバージョンでもプログレスバーに色付けしようとしています。ビューを宣言するときAndroidカスタムビューの属性がインスタンス間で共有されていますか?

<declare-styleable name="TestView"> 
     <attr name="testColor" format="color"/> 
</declare-styleable> 

::私はのようなものを持っているattrsには

<com.TestView 
    .... 
    app:testColor="#color" 
/> 

その後、私のカスタムビューは、このようなものです:

public class TestView extends ProgressBar { 

    public TestView(Context context) { 
     super(context); 
     applyTint(Color.WHITE); 
    } 

    public TestView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TestView, 0, 0); 

     try { 
      applyTint(attributes.getColor(R.styleable.TestView_testColor, Color.WHITE)); 
     } finally { 
      attributes.recycle(); 
     } 

    } 

    public TestView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, android.support.design.R.style.Base_Widget_AppCompat_ProgressBar); 
     TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TestView, 0, 0); 

     try { 
      applyTint(attributes.getColor(R.styleable.TestView_testColor, Color.WHITE)); 
     } finally { 
      attributes.recycle(); 
     } 
    } 

    private void applyTint(int color) { 
     if (Build.VERSION.SDK_INT >= 21) { 
      setIndeterminateTintList(ColorStateList.valueOf(color)); 
     } else { 
      getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN); 
     } 
    } 
} 

私が持っている問題があります色の属性が何らかの形でTestViewのインスタンス間で共有されているようです。 各ビューに独自の属性値を保持するにはどうすればよいですか?

その後編集:6 Android上で正常に動作するようですが、4.4.2

+0

その問題はすべてのSDKに存在しますか? –

+0

Android 5.0より古いバージョンに関連しているようです。今、私の4.4.2テストデバイスで発生します – Alin

答えて

0

に失敗したが、基本的に私は私の色合いがでコードを適用する更新する必要がありましたhttps://stackoverflow.com/a/37434219/379865

で答えを得た:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
    DrawableCompat.setTint(DrawableCompat.wrap(getIndeterminateDrawable()), color); 
else { 
    DrawableCompat.wrap(getIndeterminateDrawable()).mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN); 
} 
関連する問題