【发布时间】:2023-03-05 13:22:01
【问题描述】:
我知道如何将主题应用到整个应用程序,但我应该去哪里将主题应用到单个活动?
【问题讨论】:
我知道如何将主题应用到整个应用程序,但我应该去哪里将主题应用到单个活动?
【问题讨论】:
您可以通过在清单文件中的<activity> 中包含android:theme 来将主题应用于任何活动。
例如:
<activity android:theme="@android:style/Theme.Dialog"><activity android:theme="@style/CustomTheme">如果您想以编程方式设置主题,请在 onCreate() 方法中调用 setContentView() 和 super.onCreate() 方法之前使用 setTheme()。
【讨论】:
tools:context= ".YourAtivityName"
在 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 范围内设置(所有活动):
<application
android:theme="@android:style/Theme.Holo"
android:theme="@style/MyTheme">
在 Manifest.xml 的 Activity 范围内设置(单个活动):
<activity
android:theme="@android:style/Theme.Holo"
android:theme="@style/MyTheme">
要构建自定义主题,您必须在 theme.xml 文件,并在styles.xml 文件中设置样式。
【讨论】:
android:theme 属性?
android:theme="@android:style/Theme.Holo" 是添加 Android 内置主题的语法。 android:theme="@style/MyTheme" 是在 styles.xml 文件中描述的添加自定义主题的语法。在您实际的 AndroidManifest.xml 文件中,您只能为每个部分使用一个或另一个,而不是两者。
styles.xml 中创建一个空白主题,然后使用语法android:theme=@style/MyBlankTheme。
在您致电setContentView() 之前,请致电setTheme(android.R.style...) 并将...替换为您想要的主题(Theme、Theme_NoTitleBar 等)。
或者如果您的主题是自定义主题,则替换整个主题,这样您就会得到setTheme(yourThemesResouceId)
【讨论】: