Android的应用主题的自定义项目

Android的应用主题的自定义项目

本文介绍了Android的应用主题的自定义项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够宣布主题和特定的按钮设计:

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?

我试过如下:

<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的属性。

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.

推荐答案

您可以从 styles.xml MyTheme的声明$ C>,轻松地设置 @风格/ Button.b1 的android:您所选择的按钮样式属性: )

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>

这篇关于Android的应用主题的自定义项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:59