所以我有一个 Activity
(比如 TestActivity
),它需要充当普通的非主题 Activity
以及 Theme.Dialog
在其他地方.我正在尝试为这两个任务重用相同的 TestActivity
.
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.
我正在寻找动态设置主题.代码很简单:这是我的活动的 onCreate
,适用于黑色背景
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);
.....
这是清单条目
<activity android:name=".TestActivity"/>
同时我发现一个帖子说它不能在这里完成是帖子 http://code.google.com/p/android/issues/detail?id=4394 .但是有一种强烈的感觉是可以做到的.
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.
欢迎所有建议.
想解决这个问题.
问题:如何使用相同的活动作为对话框和全屏.
Problem : How to use the same activity as both dialog and full screen based.
解决方案:
@android:style/Theme.Dialog
.Java
文件中,检查定义 dialog
模式的 intent
额外内容.Theme
设置为 android.R.style.Theme
.这是默认的 theme
,如果您未定义任何主题,则会应用它.@android:style/Theme.Dialog
.Java
file, check for an intent
extra that defines dialog
mode. Theme
to android.R.style.Theme
. This is the default theme
which is applied if you do not define any theme.代码:
boolean fDialogMode = getIntent().hasExtra("dialog_mode");
if( ! fDialogMode ) {
super.setTheme(android.R.style.Theme);
}
替代解决方案:
一个更复杂的解决方案是使用 AlertDialog
如下:
A more complex solution is to use AlertDialog
as below:
ArrayAdapter
扩展而来的 ListAdapter
类.在getCount
函数中返回1
ListAdapter
class extended from ArrayAdapter
. return 1
in getCount
function
@Override
public int getCount() { return 1; }
在getView
函数中,inflate
你需要的activity
的layout
并做任何在返回 view
之前进行自定义.
In the getView
function, inflate
the layout
of the activity
you need and do any customization before returning the 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
中进行过多处理,这绝对是第二选择.
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.
考虑此解决方案的唯一原因可能是在 dialog
中显示它的逻辑与用作对话框的地方隔离.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!