Android:如何以编程方式将Activity的主题设置为Theme.Dialog [英] Android:How to programmatically set an Activity's theme to Theme.Dialog

查看:298
本文介绍了Android:如何以编程方式将Activity的主题设置为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 。但是有一种强烈的感觉可以做到。



欢迎所有的建议。

解决方案

想要解决这个问题。



问题:如何使用相同的活动作为对话框和全屏幕。



解决方案:


  1. 定义您的活动在您的AndroidManifest.xml中,主题为 @android:style / Theme.Dialog

  2. 在您各自的 .Java 文件,检查定义对话框模式的意图 extra。

  3. 如果不存在,请将主题设置为 android.R.style.Theme 。如果您没有定义任何主题,这是默认的主题

代码:

  boolean fDialogMode = getIntent()。hasExtra(dialog_mode); 

if(!fDialogMode){
super.setTheme(android.R.style.Theme);
}

替代解决方案:



更复杂的解决方案是使用 AlertDialog 如下:


  1. 定义一个从 ArrayAdapter 中扩展的 ListAdapter 类。

  2. 返回 1 getCount function

      @Override 
    public int getCount(){return 1; }


  3. 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 :

  1. Define your activity in your AndroidManifest.xml with the theme @android:style/Theme.Dialog
  2. In your respective .Java file, check for an intent extra that defines dialog mode.
  3. If it does not exist, set the Theme to android.R.style.Theme. This is the default theme 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:

  1. Define a ListAdapter class extended from ArrayAdapter.
  2. return 1 in getCount function

    @Override
    public int getCount() { return 1; }
    

  3. 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;
    }
    

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆