问题
i am able to declare a theme and a specific button design:
<style name="MyTheme" parent="android:style/Theme.Black">
<item name="android:buttonStyle">@style/Button.b1</item>
</style>
<style name="Button.b1" parent="android:style/Widget.Button">
<item name="android:textColor">#000000</item>
</style>
The problem is that this style is applied to all buttons. I would like to declare a specific button with his own style changing each theme independent of the other buttons (something like a "save"-button). Any idea?
I tried the following:
<style name="Button.MyButton" parent="android:style/Widget.Button">
<item name="android:background">@drawable/shape</item>
</style>
<style name ="Button.MyButton.Theme1">
<item name="android:textColor">#000000</item>
</style>
<style name ="Button.MyButton.Theme2">
<item name="android:textColor">#FFFFFF</item>
</style>
<Button
android:id="@+id/save_button"
android:layout_width="0px"
style="@style/Button.MyButton"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/save"/>
Now the button is independent from the theme, but it should also apply the style attribute that I declared above. At the moment it only applies the attributes that are declared in the MyButton scope and not these in MyButton.Theme1 or MyButton.Theme2.
回答1:
If your theme MyTheme
is used only to define the button style, remove it; also remove the parent
property from the button:
<style name="Button.b1">
<item name="android:textColor">#000000</item>
</style>
Then in the layout, use the style only for those button you need to, like below:
<Button
android:id="@+id/btn_custom"
style="@style/Button.b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</Button>
回答2:
You can remove MyTheme
declaration from your styles.xml
and easily set @style/Button.b1
in the android:style
property of your button of choice :)
edit: I think I finally got what you're asking for. I've no tried myself but it should work:
<!-- Declare your themes here -->
<style name="Theme1" parent="android:style/Theme.Black"></style>
<style name="Theme2" parent="android:style/Theme.Black"></style>
<!-- Declare your "abstract" Button style -->
<style name="MyButton" parent="android:style/Widget.Button">
<item name="android:background">@drawable/shape</item>
</style>
<!-- Override MyButton style for Theme1 -->
<style name ="Theme1.MyButton" parent="@style/MyButton">
<item name="android:textColor">#000000</item>
</style>
<!-- Override MyButton style for Theme2 -->
<style name ="Theme2.MyButton" parent="@style/MyButton">
<item name="android:textColor">#FFFFFF</item>
</style>
回答3:
You should use your Button.b1
style directly only for those buttons you want to be styled that way. Use those buttons' android:style
property:
android:style="Button.b1"
来源:https://stackoverflow.com/questions/10295430/android-apply-a-theme-on-a-custom-item