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

发布于 2024-10-17 02:50:18 字数 803 浏览 3 评论 0原文

所以我有一个 Activity (例如 TestActivity),它需要充当正常的无主题 Activity 以及 Theme.Dialog< /code> 在其他地方。我试图为这两个任务重用相同的 TestActivity

我正在寻找动态设置主题。 代码很简单: 这是我的活动的 onCreate ,它适用于黑色背景

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 。但是有一种强烈的感觉,这是可以做到的。

欢迎所有建议。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

倒带 2024-10-24 02:50:18

想解决这个问题。

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

解决方案:

  1. 在 AndroidManifest.xml 中使用主题 @android:style/Theme.Dialog 定义您的活动
  2. 在各自的 .Java 文件中,检查 intent 额外定义了dialog 模式。
  3. 如果不存在,请将主题设置为android.R.style.Theme。这是默认主题,如果您未定义任何主题,则会应用该主题。

代码:

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

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

替代解决方案:

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

  1. 定义一个从 ArrayAdapter 扩展的 ListAdapter 类。
  2. getCount函数中返回1

    <前><代码>@Override
    公共 int getCount() { 返回 1; }

  3. getView 函数中,inflate您需要的 activitylayout 并在返回 view 之前进行任何自定义。

    <前><代码>@Override
    public View getView( int 位置, View 视图, ViewGroup 组 ) {
    视图 v = 视图;
    if( v == null ) {
    v = getSystemService(Context.LAYOUT_INFLATER_SERVICE).inflate( <布局资源 ID>, null );
    }

    ...在这里进行任何定制....

    返回v;
    }

如果您没有在 activity class 中进行太多处理,这绝对是第二个选择,这可能是一个选项。

考虑此解决方案的唯一原因可能是在对话框中显示它的逻辑被隔离到将其用作对话框的位置。

这两种选择都对我有用,但出于显而易见的原因,我选择了第一种选择。 :-)

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

小瓶盖 2024-10-24 02:50:18

您可以在调用 setContentView(...)super.oncreate() 之前使用 setTheme(..) ,它应该可以正常工作

you can use setTheme(..) before calling setContentView(...)and super.oncreate() and it should work fine

╄→承喏 2024-10-24 02:50:18

与其他几个一样,在 onCreate 中调用 setTheme (在调用 super.onCreate 之前或之后)不起作用。但是,通过重写 setTheme,我能够指定 Manifest.xml 中指定的主题之外的主题。具体来说,以下内容没有问题:

@Override
public void setTheme(int resid) {
    boolean changeTheme = true;
    super.setTheme(changeTheme ? android.R.style.Theme_Dialog : resid);
}

我在讨论中找到了上述内容: https://code.google.com/p/android/issues/detail?id=4394

Like several others, calls to setTheme in onCreate (before or after my call to super.onCreate) did not work. However, by overriding setTheme, I was able to specify a theme other than that stated in Manifest.xml. Specifically, the following worked without issue:

@Override
public void setTheme(int resid) {
    boolean changeTheme = true;
    super.setTheme(changeTheme ? android.R.style.Theme_Dialog : resid);
}

I found the above in the discussion at: https://code.google.com/p/android/issues/detail?id=4394

北笙凉宸 2024-10-24 02:50:18

在调用 super.onCreate(savedInstance) 之前使用 setTheme

use setTheme before calling super.onCreate(savedInstance)

2024-10-24 02:50:18

这可能不适用于您的情况,但您可以使用主题:

Theme.Holo.DialogWhenLarge

当屏幕较大时,它会将您的活动显示为对话框,而当屏幕较小时,它将显示为常规活动。
对话框 上的 Android 文档对此进行了介绍,并且还包含信息编写一个也可以作为全屏片段显示的对话框。

This may not be applicable in your situation, but you can use the theme:

Theme.Holo.DialogWhenLarge

and it will display your activity as a dialog when the screen is large, and as a regular activity when the screen is small.
This is covered in the Android documentation on Dialogs and also contains information on programming a Dialog which can also sun as a full screen fragment.

失与倦" 2024-10-24 02:50:18

默认主题库调用:

super.setTheme(android.R.style.Theme);

在我的例子中,我使用的是 AppCompat 主题,因此请确保在您的 id 上引用正确的库(即):

super.setTheme(android.support.v7.appcompat.R.style.Theme_AppCompat_NoActionBar);

Default theme library call:

super.setTheme(android.R.style.Theme);

In my case I was using AppCompat Theme, so make sure that on your id you refer the proper library (i.e.):

super.setTheme(android.support.v7.appcompat.R.style.Theme_AppCompat_NoActionBar);
无远思近则忧 2024-10-24 02:50:18

我知道我迟到了,但我想在这里发布一个解决方案:

检查完整的源代码 这里。这是我更改主题时使用的代码...

SharedPreferences pref = PreferenceManager
       .getDefaultSharedPreferences(this);
String themeName = pref.getString("prefSyncFrequency3", "Theme1");
if (themeName.equals("Africa")) {
    setTheme(R.style.AppTheme);
} else if (themeName.equals("Colorful Beach")) {
    //Toast.makeText(this, "set theme", Toast.LENGTH_SHORT).show();
    setTheme(R.style.beach);
} else if (themeName.equals("Abstract")) {
    //Toast.makeText(this, "set theme", Toast.LENGTH_SHORT).show();
    setTheme(R.style.abstract2);
} else if (themeName.equals("Default")) {
    setTheme(R.style.defaulttheme);
}

I know that I am late but I would like to post a solution here:

Check the full source code here. This is the code I used when changing theme...

SharedPreferences pref = PreferenceManager
       .getDefaultSharedPreferences(this);
String themeName = pref.getString("prefSyncFrequency3", "Theme1");
if (themeName.equals("Africa")) {
    setTheme(R.style.AppTheme);
} else if (themeName.equals("Colorful Beach")) {
    //Toast.makeText(this, "set theme", Toast.LENGTH_SHORT).show();
    setTheme(R.style.beach);
} else if (themeName.equals("Abstract")) {
    //Toast.makeText(this, "set theme", Toast.LENGTH_SHORT).show();
    setTheme(R.style.abstract2);
} else if (themeName.equals("Default")) {
    setTheme(R.style.defaulttheme);
}
舞袖。长 2024-10-24 02:50:18
setTheme(android.R.style.Theme_Dialog);
setTheme(android.R.style.Theme_Dialog);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文