整个应用程序的通用样式

时间:2014-08-01 13:50:52

标签: android xml styles manifest

我一直在努力让我的应用看起来“不错”。关于该主题的上一篇文章链接到一个非常好的网站,解释了如何创建一个单一的样式文件,有效地用我自己的例子覆盖默认主题元素。将基本“按钮”设置为绿色背景,而不必为每个按钮使用“样式”或“背景”属性。

我试图实现这个但它似乎没有用 - 这就是我在我的清单中所拥有的:

android:theme="@style/spyEyeTheme" >

这是单个文件:

<style name="spyEyeTheme" parent="android:Theme.Light">

    <item name="android:buttonStyle">@style/myButton</item>
    <item name="android:textViewStyle">@style/myTextView</item>
    <item name="android:editTextStyle">@style/myEditText</item>

</style>

<style name="myTextView" parent="android:Theme.Light">
    <item name="android:textColor">#FF0000</item>
    <item name="android:textAppearance">?android:attr/textAppearanceLarge</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>

</style>

<style name="myEditText" parent="android:Theme.Light">
    <item name="android:textColor">#FFFFFF</item>
    <!-- item name="android:textSize">30sp</item> -->
    <item name="android:textAppearance">?android:attr/textAppearanceLarge</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">@color/lime</item>
</style>

<style name="myButton" parent="android:Theme.Light">

    <item name="android:background">#008000</item>  <--- green background for button

</style>

理论上,如果我现在在XML文件中定义一个按钮,它应该生成一个绿色背景的按钮:

<Button
    android:id="@+id/loadBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/newBtn"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp"
    android:onClick="loadGame"
    android:text="@string/load"
    android:textSize="30sp"
    android:typeface="sans" />

不幸的是,事实并非如此。没有错误,但按钮正在显示标准灰色背景(其他属性也不起作用)。就好像系统无法找到或忽略我的样式重定向。

有什么想法吗?

编辑:好的,让它工作,但不知道为什么。在Eclipse中,在我的XML文件的“Graphical Layout”选项卡上,我注意到屏幕顶部有一个下拉列表,显示了“AppTheme”。下拉有一个选项可以改为我的'spyEyeTheme',我做了,现在它正在工作。

但是,我看不到我的代码有任何变化 - 有人可以解释发生了什么吗?

1 个答案:

答案 0 :(得分:0)

我认为你的按钮样式应该继承自Widget.Button

<style name="myButton" parent="android:style/Widget.Button">
    <item name="android:background">#008000</item>  <--- green background for button
</style>