Android:如何以编程方式将Activity的主题设置为Theme.Dialog [英] Android:How to programmatically set an Activity's theme to Theme.Dialog
问题描述
所以我有一个活动
(说 TestActivity
),需要作为一个正常的没有化的活动
以及其他地方的 Theme.Dialog
。我试图为这两个任务重复使用相同的 TestActivity
。
我正在寻找动态设置主题。
代码很简单:
这是我的活动的 onCreate
,使用黑色背景
public void onCreate(Bundle icicle){
if(Utility.isDialog == true)
setTheme(android.R.style.Theme_Dialog);
super.onCreate(冰柱);
requestWindowFeature(Window.FEATURE_NO_TITLE);
.....
这里是清单条目
< activity android:name =。TestActivity/>
在此期间,我发现一篇文章说,这里不能完成的是< a href =http://code.google.com/p/android/issues/detail?id=4394 =nofollow noreferrer> http://code.google.com/p/android/issues/detail ?id = 4394 。但是有一种强烈的感觉可以做到。
欢迎所有的建议。
想要解决这个问题。
问题:如何使用相同的活动作为对话框和全屏幕。
解决方案:
- 定义您的活动在您的AndroidManifest.xml中,主题为
@android:style / Theme.Dialog
- 在您各自的
.Java
文件,检查定义对话框
模式的意图
extra。
- 如果不存在,请将
主题
设置为android.R.style.Theme
。如果您没有定义任何主题,这是默认的主题
。
代码:
boolean fDialogMode = getIntent()。hasExtra(dialog_mode);
if(!fDialogMode){
super.setTheme(android.R.style.Theme);
}
替代解决方案:
更复杂的解决方案是使用 AlertDialog
如下:
- 定义一个从
ArrayAdapter
中扩展的ListAdapter
类。 -
返回
1
在getCount
function@Override
public int getCount(){return 1; }
-
在
getView
函数中,inflate
您需要和执行的活动
的布局
在返回视图
之前的任何定制。@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);
}
...在这里做任何定制....
return v;
}
第二选择选项,如果你在活动中没有太多的处理
class
这可能是一个选项。 p>
只有考虑这个解决方案的原因可能是在对话框
中显示它的逻辑是孤立到它的地方用作对话框。
这两个选项都适用于我,但由于显而易见的原因,我采用了第一个选项。 : - )
So I have an Activity
(say TestActivity
) which needs to act as a normal unthemed Activity
as well as a Theme.Dialog
at other place. I am trying to reuse same TestActivity
for both the tasks.
All I am looking for setting the theme dynamically.
The code is simple:
Here is my activity's onCreate
that works with a black background
public void onCreate(Bundle icicle) {
if (Utility.isDialog == true)
setTheme(android.R.style.Theme_Dialog);
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
.....
and here is the Manifest Entry
<activity android:name=".TestActivity"/>
And in the meantime I found a post that says it can't be done here is the post http://code.google.com/p/android/issues/detail?id=4394 .But there is a strong feeling that it can be done.
All suggestions are welcome.
Would like to give a work around for this problem.
Problem : How to use the same activity as both dialog and full screen based.
Solution :
- Define your activity in your AndroidManifest.xml with the theme
@android:style/Theme.Dialog
- In your respective
.Java
file, check for anintent
extra that definesdialog
mode. - If it does not exist, set the
Theme
toandroid.R.style.Theme
. This is the defaulttheme
which is applied if you do not define any theme.
Code :
boolean fDialogMode = getIntent().hasExtra("dialog_mode");
if( ! fDialogMode ) {
super.setTheme(android.R.style.Theme);
}
Alternate Solution:
A more complex solution is to use AlertDialog
as below:
- Define a
ListAdapter
class extended fromArrayAdapter
. return
1
ingetCount
function@Override public int getCount() { return 1; }
In the
getView
function,inflate
thelayout
of theactivity
you need and do any customization before returning theview
.@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; }
This is definitely a second choice option by if you are not doing too much processing in the activity
class
this could be an option.
Only reason to consider this solution could be that the logic to show it in a dialog
is isolated to the places where it is used as a dialog.
Both the options worked for me but for obvious reasons I am taking the first option. :-)
这篇关于Android:如何以编程方式将Activity的主题设置为Theme.Dialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!