Devel/ui/applying-themes

From Android中文网

Android中文网(androidcn.net) 版权申明 : creativecommons licenses
Jump to: navigation, search

Applying a Theme to your Application

[编辑] 在你的程序中应用主题

If you do not explicitly specify a theme for your UI, Android will use the default theme defined by android.R.style.Theme. Many times you will want to use a different system theme (such as Theme.Dark) or create your own theme (as described in Style and Theme Resources).

如果你没有明确的为你的应用指定一个主题,Android系统将将使用android.R.style.Theme中定义的主题。更多时候你可能希望使用与系统主题不同的主题(例如Theme.Dark)或者自己创建一个主题(在样式或者主题资源中定义?????)。

To set your theme in XML, simply specify the desired theme in your AndroidManifest.xml file with the theme attribute. This can be used with the <application> tag (shown here) to specify a default theme for all of your activities, and/or with the <activity> to control the theme of a particular activity.

只需要在AndroidManifest.xml中增加一个主题的属性我们就可以完成主题设置。在<application>标记中可以为我们所有的activity设置同一个缺省的主题。或者分别在<activity>中设置不同的主题。需要帮助

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
       package="com.google.android.home">
   <application android:theme="@android:style/Theme.Dark" >
       <activity class=".Home"
       ...
       </activity>
   </application>
</manifest>

You can also set the theme programmatically, if needed. When doing so, be sure to set the theme before creating any views so that the correct theme is used for all of your user-interface elements. Note that this approach should typically be avoided, especially from the main activities of your application, because the theme you set here may not be used for any animations the system uses to show the activity (which is done before your application starts).

如果你真的需要你也可以在程序中制定你要使用的主题。当你向这么做的时候,你必须在所有的View创建之前设置主题,这样你的View才能正确使用主题。注意到这种做法应通常是可以避免的,如果你在你的主activity中设置,因为你的主题订定在这里,不得用于任何动画,该系统所使用的,以显示活动(在你的程序启动之前完成) 。需要帮助

protected void onCreate(Bundle icicle) {
   super.onCreate(icicle);
   ...
   setTheme(android.R.style.Theme_Dark);
   setContentView(R.layout.linear_layout_3);
}
Personal tools