2014-05-11 3 views
1

선택한 애플리케이션 테마 (어두운 색 또는 밝은 색)를 기반으로 두 가지 드로어 블 세트 (어둡고 밝은)가 있습니다. 버튼의 상태를 업데이트해야한다면 일시 중지/재생 버튼을 업데이트하십시오. 현재 테마를 알지 못해서 원본을 참조 할 수 없기 때문에 어떻게해야합니까? 예를 들어테마를 기반으로 프로그래밍 방식으로 Android 변경 버튼

, styles.xml

<style name="Theme.ServeStream.Dark" parent="@style/Theme.AppCompat"> 
    <item name="attr/ic_action_pause_over_video">@drawable/ic_action_pause_over_video_dark</item> 
    <item name="attr/ic_action_play_over_video">@drawable/ic_action_play_over_video_dark</item> 
    <item name="attr/ic_action_previous">@drawable/ic_action_previous_dark</item> 
    <item name="attr/ic_action_next">@drawable/ic_action_next_dark</item> 
</style> 

<style name="Theme.ServeStream.Light" parent="@style/Theme.AppCompat.Light"> 
    <item name="attr/ic_action_pause_over_video">@drawable/ic_action_pause_over_video_light</item> 
    <item name="attr/ic_action_play_over_video">@drawable/ic_action_play_over_video_light</item> 
    <item name="attr/ic_action_previous">@drawable/ic_action_previous_light</item> 
    <item name="attr/ic_action_next">@drawable/ic_action_next_light</item> 
</style> 

attrs.xml이 : 그러나

<attr name="ic_action_pause_over_video" format="reference" /> 
<attr name="ic_action_play_over_video" format="reference" /> 
<attr name="ic_action_previous" format="reference" /> 
<attr name="ic_action_next" format="reference" /> 

, 다음 코드는 컴파일되지 않습니다 : 당신은 자원 ID를 얻을 수 있습니다

mPauseButton.setImageResource(R.drawable.ic_action_pause_over_video); 

답변

1

을 코드에서 현재 테마를 다음과 같이 정의했습니다.

TypedValue typedvalueattr = new TypedValue(); 

getTheme().resolveAttribute(R.attr.ic_action_pause_over_video, typedvalueattr, true); 
mPauseButton.setImageResource(typedvalueattr.resourceId); 

XML 레이아웃에서 스타일이 지정된 속성을 여러 테마에 사용하는 방법은 there입니다.

+0

감사합니다. –

관련 문제