Rather than creating dynamically those buttons by calling their contructor and then setting the theme, I will inflate a button layout where I already took care of the theme, then I will set the text and click behavior by code. If you have a solution to apply the theme by code, please let me know
Use theme on views created dynamically
-
11-10-2022 - |
문제
I created a few themes for my app and everything works as intended for the xml layouts.
I declared some resources in my attrs.xml file:
<resources>
<attr name="themed_button" format="reference" />
<attr name="button_text_color" format="color|reference" />
</resources>
Then I'm using those resources in my layouts
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="?attr/themed_button"
android:textColor="?attr/button_text_color" />
finaly I defined a few styles:
<style name="Theme.green" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="themed_button">@drawable/themed_button_green</item>
<item name="button_text_color">@color/button_text_color_white</item>
</style>
<style name="Theme.red" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="themed_button">@drawable/themed_button_red</item>
<item name="button_text_color">@color/button_text_color_white</item>
</style>
Now what I want is to apply the same style on Views created dnamically. I have no clue on how to retrieve current theme resources called 'button_text_color' for exemple. Looks like I should use obtainStyledAttributes but I failed to use it...
해결책
제휴하지 않습니다 StackOverflow