问题
Android 5.0 Lollipop added the ability to set a theme for different views (not just the Activity).
android:theme="@style/MyAwesomeTheme"
Is there a way to set the theme on a view dynamically?
回答1:
android:theme changes specific attributes specified in the theme resource. You could instantiate specific view using any overloaded constructor, like View(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes). which basically is what inflater does.
For example v7.widget.Toolbar tries to create themed context out of resource android:themeright in the super call of the constructor, which is more or less what I mentioned above. Only certain widgets override application theme using android:theme.
I'm not sure if is possible to directly specify theme, rather than instantiation mentioned above, or using supported individual attribute and its related method.
来源:https://stackoverflow.com/questions/27084343/set-a-theme-on-a-view-programmatically-on-android-5-0-lollipop