将主题应用于Android中的活动?

Wil*_*lly 73 android themes manifest

我知道如何将主题应用于整个应用程序,但是我将在哪里将主题应用于单个活动?

Par*_*ani 142

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

例如:

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

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


liv*_*ove 30

要在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);
Run Code Online (Sandbox Code Playgroud)

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

 <application
    android:theme="@android:style/Theme.Holo"
    android:theme="@style/MyTheme">
Run Code Online (Sandbox Code Playgroud)

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

  <activity
    android:theme="@android:style/Theme.Holo"
    android:theme="@style/MyTheme">
Run Code Online (Sandbox Code Playgroud)

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

  • 为什么你添加了两个`android:theme`属性? (2认同)

jcw*_*jcw 8

在你打电话之前setContentView(),打电话setTheme(android.R.style...),只需用你想要的主题替换......(Theme,Theme_NoTitleBar等).

或者,如果您的主题是自定义主题,则替换整个主题,以便获得 setTheme(yourThemesResouceId)