Android:使用一个主题自定义所有UI小部件

时间:2011-10-19 22:41:28

标签: android android-manifest

如何通过创建一种样式并通过AndroidManifest中的android:theme将其应用于应用程序来自定义应用程序中的所有小部件?

2 个答案:

答案 0 :(得分:2)

这是一个可能有用的示例(它可能不会逐字逐句,因为我已经调整它以简化它,并展示您可以做的其他一些事情)。

在res \ values \ styles.xml中:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
  <style name="TextColorForTheme">
    <item name="android:textColor">@color/red</item>
  </style>
</resources>

在res \ values \ themes.xml中:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
  <style name="myTheme" parent="@style/android:Theme">
    <item name="android:listSeparatorTextViewStyle">@style/TextColorForTheme</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowFrame">@null</item>
  </style>
</resources>

然后在AndroidManifest.xml中,设置整个应用程序或单个活动以使用该主题:

  <application
    android:theme="@style/myTheme"
<snip>    
    <activity
      android:theme="@style/myTheme"
<snip>

或者,您可以在Java Activity的代码中设置主题:

  @Override
  public void onCreate(Bundle bundle)
  {
    super.onCreate(bundle);
    setTheme(R.style.myTheme);
<snip>

答案 1 :(得分:1)

您只能通过创建一种样式来完成此操作。主题本质上是一种元样式,它定义了每个可用的不同小部件的默认样式。您将首先创建一个主题(它本身就是一种样式),其中包含一个现有系统主题的父级,并为您希望从基本主题更改的每个小部件设置默认样式属性。例如,如果您想在主题中将其设置为默认的不同按钮样式,则可以将以下内容添加到主题定义中:

<item name="android:buttonStyle">@style/MyButtonStyle</item>

有关详细信息,请参阅http://developer.android.com/guide/topics/ui/themes.html