所以我有Activity
(说TestActivity
),需要在其他地方充当普通的未经训练的Activity
和Theme.Dialog
。我正在尝试为这两个任务重用相同的TestActivity
。
我正在寻找动态设置主题。
代码很简单:
这是我的活动onCreate
,适用于黑色背景
public void onCreate(Bundle icicle) {
if (Utility.isDialog == true)
setTheme(android.R.style.Theme_Dialog);
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
.....
这里是Manifest Entry
<activity android:name=".TestActivity"/>
与此同时,我发现一篇帖子说这里不能发帖http://code.google.com/p/android/issues/detail?id=4394。但是有一种强烈的感觉,可以做到。
欢迎所有建议。
答案 0 :(得分:53)
想解决这个问题。
问题:如何使用与基于对话框和全屏的相同活动。
解决方案:
@android:style/Theme.Dialog
.Java
文件中,检查定义intent
模式的dialog
个额外内容。 Theme
设置为android.R.style.Theme
。这是默认theme
,如果您没有定义任何主题,则会应用它。代码:
boolean fDialogMode = getIntent().hasExtra("dialog_mode");
if( ! fDialogMode ) {
super.setTheme(android.R.style.Theme);
}
替代解决方案:
更复杂的解决方案是使用AlertDialog
,如下所示:
ListAdapter
扩展的ArrayAdapter
类。 在1
函数
getCount
@Override
public int getCount() { return 1; }
在getView
函数中inflate
layout
activity
view
,并在返回@Override
public View getView( int position, View view, ViewGroup group ) {
View v = view;
if( v == null ) {
v = getSystemService(Context.LAYOUT_INFLATER_SERVICE).inflate( <layout res id>, null );
}
... Do any customization here ....
return v;
}
之前进行任何自定义。
activity
如果您没有在class
dialog
中进行太多处理,这绝对是第二选择。这可能是一种选择。
只考虑这个解决方案的理由可能是在{{1}}中显示它的逻辑与用作对话框的地方是隔离的。
这两个选项对我有用,但由于显而易见的原因,我选择了第一个选项。 : - )
答案 1 :(得分:45)
您可以在致电setTheme(..)
和setContentView(...)
之前使用super.oncreate()
,它应该可以正常使用
答案 2 :(得分:11)
和其他几个人一样,在onCreate中调用setTheme(在调用super.onCreate之前或之后)不起作用。但是,通过重写setTheme,我能够指定除Manifest.xml中所述之外的主题。具体来说,以下工作没有问题:
@Override
public void setTheme(int resid) {
boolean changeTheme = true;
super.setTheme(changeTheme ? android.R.style.Theme_Dialog : resid);
}
我在讨论中找到了上述内容:https://code.google.com/p/android/issues/detail?id=4394
答案 3 :(得分:10)
在致电onCreate()
之前致电setContentView()
中的Activity.setTheme()
。
答案 4 :(得分:3)
在致电setTheme
super.onCreate(savedInstance)
答案 5 :(得分:0)
这可能不适用于您的情况,但您可以使用主题:
Theme.Holo.DialogWhenLarge
它会在屏幕较大时将您的活动显示为对话框,并在屏幕较小时显示为常规活动。 这在Dialogs的Android文档中有所介绍,还包含有关编写对话框的信息,该对话框也可以作为全屏片段进行晒黑。
答案 6 :(得分:0)
默认主题库调用:
super.setTheme(android.R.style.Theme);
在我的情况下,我使用的是AppCompat主题,因此请确保在您的ID上引用正确的库(即):
super.setTheme(android.support.v7.appcompat.R.style.Theme_AppCompat_NoActionBar);
答案 7 :(得分:-1)
setTheme(android.R.style.Theme_Dialog);
答案 8 :(得分:-1)
我知道我迟到但我想在这里发布解决方案:
检查完整的源代码here。这是我在更改主题时使用的代码......
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(this);
String themeName = pref.getString("prefSyncFrequency3", "Theme1");
if (themeName.equals("Africa")) {
setTheme(R.style.AppTheme);
} else if (themeName.equals("Colorful Beach")) {
//Toast.makeText(this, "set theme", Toast.LENGTH_SHORT).show();
setTheme(R.style.beach);
} else if (themeName.equals("Abstract")) {
//Toast.makeText(this, "set theme", Toast.LENGTH_SHORT).show();
setTheme(R.style.abstract2);
} else if (themeName.equals("Default")) {
setTheme(R.style.defaulttheme);
}