Android的:如何编程设置一个活动的主题Theme.Dialog [英] Android:How to programmatically set an Activity's theme to Theme.Dialog

查看:219
本文介绍了Android的:如何编程设置一个活动的主题Theme.Dialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个活动(说TestActivity),需要作为一个正常的unthemed活动,以及一个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.

所有我找的动态设置的主题。 在code是简单的: 这是我的活动的的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"/>

而在此期间,我发现了一个帖子,说不能在这里做的是后期<一href="http://$c$c.google.com/p/android/issues/detail?id=4394">http://$c$c.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.

所有的建议都欢迎。

推荐答案

想给一个工作围绕这个问题。

Would like to give a work around for this problem.

问题:如何使用相同的活性对话框和全屏幕基于

Problem : How to use the same activity as both dialog and full screen based.

解决方法:

  1. 定义你的活动在AndroidManifest.xml中,主题@android:款式/ Theme.Dialog
  2. 在各自的.java文件,检查一个额外的意图定义对话框模式。
  3. 如果它不存在,设置主题为android.R.style.Theme。这是一个应用,如果你不定义任何主题的默认主题。

code:

boolean fDialogMode     = getIntent().hasExtra( "dialog_mode" );

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

另一种解决方案:

Alternate Solution:

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

A more complex solution is to use AlertDialog as below:

  1. 定义从ArrayAdapter延伸的ListAdapter类。
  2. 在getCount将函数返回1

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

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

  • 在getView功能,抬高你所需要的活动的布局和返回之前的观点做任何定制。

  • 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的:如何编程设置一个活动的主题Theme.Dialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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