94

我知道如何将主题应用到整个应用程序,但我该去哪里将主题应用到单个活动?

4

3 回答 3

170

android:theme您可以通过包含在<activity>清单文件中来将主题应用于任何活动。

例如:

  1. <activity android:theme="@android:style/Theme.Dialog">
  2. <activity android:theme="@style/CustomTheme">

如果您想以编程方式设置主题,请setTheme()在调用setContentView()super.onCreate()方法内部的onCreate()方法之前使用。

于 2013-10-01T20:25:58.940 回答
36

在 Activity.java 中以编程方式设置它:

public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setTheme(R.style.MyTheme); // (for Custom theme)
  setTheme(android.R.style.Theme_Holo); // (for Android Built In Theme)

  this.setContentView(R.layout.myactivity);

在 Manifest.xml 中的应用程序范围中设置(所有活动):

 <application
    android:theme="@android:style/Theme.Holo"
    android:theme="@style/MyTheme">

在 Manifest.xml 中设置活动范围(单个活动):

  <activity
    android:theme="@android:style/Theme.Holo"
    android:theme="@style/MyTheme">

要构建自定义主题,您必须在 theme.xml 文件中声明主题,并在 styles.xml 文件中设置样式。

于 2014-10-23T16:46:49.733 回答
8

在您致电之前setContentView(),请致电setTheme(android.R.style...)并将 ... 替换为您想要的主题(Theme、Theme_NoTitleBar 等)。

或者,如果您的主题是自定义主题,则替换整个主题,这样您就得到了setTheme(yourThemesResouceId)

于 2013-10-01T20:21:52.050 回答