Android applies theme to user element

I can 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 applies to all buttons. I would like to declare a specific button with my own style, changing each theme independently of the other buttons (something like the "save" button). Any ideas?


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 does not depend on the theme, but it should also apply the style attribute that was mentioned above. Currently, it only applies attributes declared in the MyButton scope, not in MyButton.Theme1 or MyButton.Theme2.

+3
source share
3 answers

MyTheme , ; parent :

<style name="Button.b1">
    <item name="android:textColor">#000000</item>
</style>

, , :

 <Button
         android:id="@+id/btn_custom"
         style="@style/Button.b1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" >
 </Button>
+3

MyTheme styles.xml @style/Button.b1 android:style :)

edit: , , . , :

<!-- 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>
+1

Button.b1 , . android:style:

android:style="Button.b1"
0

All Articles