2012-04-24 26 views
3

我能够宣布主题和特定的按钮设计:的Android应用主题的自定义项目

<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> 

的问题是,这种风格应用到所有按钮。我想用自己的风格来声明一个特定的按钮,以独立于其他按钮来改变每个主题(就像“保存”按钮一样)。任何想法?


我试过如下:

<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"/> 

现在按钮是独立于主题,但它应该也适用,我上面定义的样式属性。目前它只应用MyButton范围中声明的属性,而不适用于MyButton.Theme1或MyButton.Theme2中的属性。

+0

为每个按钮创建单独的主题... – 2012-04-24 09:44:51

+0

我正在做类似的事情! http://stackoverflow.com/questions/17103894/overriding-referenced-style-attributes – toobsco42 2013-06-14 08:12:08

回答

1

您可以从styles.xml删除MyTheme声明,并很容易地在你的选择:)

编辑按钮的android:style属性设置@style/Button.b1:我想,我终于得到了你问什么。我没有尝试过自己,但它应该工作:

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

这不完全是我想要的 - 我更新了我的问题。 – Anthea 2012-04-24 10:14:08

0

你应该用你Button.b1风格直接只为你希望被称呼这样的按钮。使用这些按钮android:style属性:

android:style="Button.b1" 
3

如果你的主题MyTheme只是用来定义按钮样式,将其删除;在布局

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

然后,使用风格只为那些需要像下面的按钮:还从按钮移除parent财产

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

Mytheme在这里比较大,但它应该包含一个特定的样式元素,其中包含仅用于保存按钮的信息。如果我改变主题,保存按钮也应该以他自己的方式改变。 – Anthea 2012-04-24 10:11:23

+0

这并不意味着您必须在MyTheme中设置“保存”按钮样式。你如何改变主题?编程? – Adinia 2012-04-24 10:17:07

+0

因为它应该在运行时,我想我会在每个活动的开始以编程方式执行此操作。 – Anthea 2012-04-24 10:18:54