Android 如何在运行时将主题设置为活动?在 onCreate 和 setContentView 之前调用 setTheme 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11134943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to setTheme to an activity at runtime? It doesn't work call setTheme before onCreate and setContentView
提问by herman brain
I want setTheme
to an activity at runtime, I have search some solutions by google.
someone said call setTheme
before onCreate and setContentView can works, the code section like
我想setTheme
在运行时进行活动,我通过谷歌搜索了一些解决方案。有人说setTheme
在 onCreate 和 setContentView 之前调用可以工作,代码部分像
public void onCreate(Bundle savedInstanceState) {
setTheme(android.R.style.Theme_Translucent_NoTitleBar);
super.onCreate(savedInstanceState);
...
setContentView(...)
}
but it not works, I want to know, is there another solution can setTheme to activity?
但它不起作用,我想知道,是否有另一种解决方案可以将主题设置为活动?
回答by Praveenkumar
Just try this - set your theme after super.onCreate(savedInstanceState);
and before setContentView(...)
试试这个 - 在super.onCreate(savedInstanceState);
前后设置你的主题setContentView(...)
Like below code -
像下面的代码 -
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Translucent_NoTitleBar); // Set here
setContentView(...)
}
回答by Chris.Jenkins
Actually this only worked for me if I set it before calling super.onCreate(savedInstanceState);
实际上,如果我在打电话之前设置它,这只对我有用 super.onCreate(savedInstanceState);
public void onCreate(Bundle savedInstanceState)
{
final int themeRes = getIntent().getIntExtra(EXTRA_THEME_ID, 0);
if (themeRes != 0) setTheme(themeRes);
super.onCreate(savedInstanceState);
//ect...
}
回答by Umesh
setContentView(...);
setTheme(R.style.MyTheme);
setContentView(...);
It must work well..
它必须运作良好..
More on Themes, Read this http://entertheinfinity.blogspot.in/2016/06/designing-android-interface-themes.html
有关主题的更多信息,请阅读 http://entertheinfinity.blogspot.in/2016/06/designing-android-interface-themes.html
回答by Eric
To set the theme at runtime and fix the "black background" problem:
在运行时设置主题并修复“黑色背景”问题:
the theme should be set before
onCreate()
.public void onCreate(Bundle savedInstanceState) { setTheme(android.R.style.Theme_Translucent_NoTitleBar); super.onCreate(savedInstanceState); ... setContentView(...) }
the theme of the transparent activity in the android manifest should be set to any theme that has a transparent background (e.g. a dialog theme).
this tells the Android OS continue to draw the activities behind the transparent activity so that you don't end up with a black background.
i'm using an
AppCompatActivity
; i need to use anAppCompat
theme:<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.app"> ... <application ...> ... <activity android:name=".TranslucentActivity" android:theme="@style/Theme.AppCompat.DayNight.Dialog" .../> ... </application> </manifest>
主题应该是之前设定的
onCreate()
。public void onCreate(Bundle savedInstanceState) { setTheme(android.R.style.Theme_Translucent_NoTitleBar); super.onCreate(savedInstanceState); ... setContentView(...) }
android manifest 中透明活动的主题应该设置为任何具有透明背景的主题(例如对话框主题)。
这告诉 Android 操作系统继续在透明活动后面绘制活动,这样您就不会以黑色背景结束。
我正在使用
AppCompatActivity
; 我需要使用一个AppCompat
主题:<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.app"> ... <application ...> ... <activity android:name=".TranslucentActivity" android:theme="@style/Theme.AppCompat.DayNight.Dialog" .../> ... </application> </manifest>