2017-01-26 6 views
0
の場合はnullを返します

マイテーマ:resolveAttributeは常に属性

<style name="AppThemeDark" parent="Theme.AppCompat"> 
    <item name="AppButton">@drawable/standard_dark_button_selector</item> 
</style>  

私はテーマに描画可能な取得するために使用このコード:

public static Drawable getThemedDrawable(Context context, int resource) { 
    TypedValue typedValue = new TypedValue(); 
    Resources.Theme theme = context.getTheme(); 
    theme.resolveAttribute(resource, typedValue, true); 
    if(typedValue.resourceId == 0) { 
     return context.getResources().getDrawable(R.drawable.standard_theme_not_found); 
    } 
    else 
     return context.getResources().getDrawable(typedValue.resourceId); 
} 

をそして、これが直接の呼び出しです:

positive.setBackgroundDrawable(Theme.getThemedDrawable(getBaseContext(), R.attr.AppButton)); 

typedValue.resourceIdは常に0で、実際の描画可能リソースIDを返しません。この場合、常にR.drawable.standard_theme_not_fが返されます。オンド。

私はこのコードを(typedValue.dataから来た)色で使用し、その塗りつぶしがうまく動作します。

どうすればこの問題を解決できますか?

答えて

-1

私はそのテーマがテーマの中で適切に設定されているという重要な答えを見つけました。

あなたはプログラムでテーマを適用する場合:

その後、テーマは

はそうあなたが

Theme.getThemedDrawable(myActivity, R.attr.AppContainer1Background) 

代わりの

を使用する必要がありますgetBaseContext()内に保存されていない
setTheme(myTheme); 

Theme.getThemedDrawable(getBaseContext(), R.attr.AppContainer1Background) 
関連する問題