Android:How to programmatically set an Activity#39;s theme to Theme.Dialog(Android:如何以编程方式将 Activity 的主题设置为 Theme.Dialog)
问题描述
所以我有一个 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.
解决方案:
- 在您的 AndroidManifest.xml 中定义您的活动,主题为
@android:style/Theme.Dialog - 在您各自的
.Java文件中,检查定义dialog模式的intent额外内容. - 如果不存在,请将
Theme设置为android.R.style.Theme.这是默认的theme,如果您未定义任何主题,则会应用它.
- Define your activity in your AndroidManifest.xml with the theme
@android:style/Theme.Dialog - In your respective
.Javafile, check for anintentextra that definesdialogmode. - If it does not exist, set the
Themetoandroid.R.style.Theme. This is the defaultthemewhich 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
- Define a
ListAdapterclass extended fromArrayAdapter. return
1ingetCountfunction
@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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android:如何以编程方式将 Activity 的主题设置为 Theme.Dialog
基础教程推荐
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
