Android:如何以编程方式将 Activity 的主题设置为 Theme.Dialog

Android:How to programmatically set an Activity#39;s theme to Theme.Dialog(Android:如何以编程方式将 Activity 的主题设置为 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.

解决方案:

  1. 在您的 AndroidManifest.xml 中定义您的活动,主题为 @android:style/Theme.Dialog
  2. 在您各自的 .Java 文件中,检查定义 dialog 模式的 intent 额外内容.
  3. 如果不存在,请将 Theme 设置为 android.R.style.Theme.这是默认的 theme,如果您未定义任何主题,则会应用它.
  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.

代码:

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:

  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函数中,inflate你需要的activitylayout并做任何在返回 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

    相关文档推荐

    How To Create a Rotating Wheel Control?(如何创建转轮控件?)
    How to avoid restarting activity when orientation changes on Android(如何在 Android 上的方向更改时避免重新启动活动)
    Screen orientation lock(屏幕方向锁定)
    Strange behavior with android orientation sensor(android方向传感器的奇怪行为)
    Android: Rotate image in imageview by an angle(Android:将imageview中的图像旋转一个角度)
    Activity restart on rotation Android(旋转Android上的活动重启)