如何以编程方式将活动主题化为对话框? [英] how to programatically theme an activity to be like a dialog?

查看:231
本文介绍了如何以编程方式将活动主题化为对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式(不触及 AndroidManifext.xml )设置<的主题code>活动 以便它看起来像一个对话框

How does one programatically (without touching the AndroidManifext.xml) set the theme of an Activity so that it looks like a dialog?

注意:我可以修改 AndroidManifext.xml ,只要不需要修改它为了在看起来像普通活动或对话框之间切换。

Note: I am ok with modifying the AndroidManifext.xml as long as it does not need to be modified in order to switch between making it look like a normal activity or a dialog.

我根据此stackoverflow答案尝试了以下操作:

I tried the following as per this stackoverflow answer:

public class DialogActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        setTheme(android.R.style.Theme_DeviceDefault_Dialog);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        Log.d(TAG,"Build.VERSION.SDK_INT: "+Build.VERSION.SDK_INT); // 23
    }
}

但最终将背景中的所有内容涂黑

我还看到此stackoverflow答案,并尝试:

public class DialogActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        setTheme(android.R.style.Theme_DeviceDefault_Dialog);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    }
}

但它最终会使一切都是黑色的。

怎么办?谢谢。

推荐答案

背景



AndroidManifest.xml Acivity 后面显示>活动 >是一个对话;否则android os将在它后面绘制 Activity (可能是为了节省内存,因为它通常不会被看到)。

Background

The Activity behind an Acivity is drawn if the foreground activity's theme according to its AndroidManifest.xml is a dialog; otherwise the android os will not draw the Activity behind it (probably to save memory since it usually won't be seen anyway).

为了利用这一点,我们将 Acitvity 的主题设置为清单中的对话框,使得android os绘制 Activity 在它后面,但稍后,以编程方式将我们的 Activity 的主题设置为我们在运行时喜欢的任何内容。

To exploit this, we set the theme of our Acitvity to a dialog in the manifest, making the android os draw the Activity behind it, but later, programatically set our Activity's theme to whatever we like at runtime.

我做了一个例子并把它放了在github上。

第1步:创建两个您的应用程序的自定义主题在 styles.xml 中。一个用于正常活动,另一个用于对话活动。自定义对话框主题从基础主题(也是对话框)继承非常重要。在我的例子中,父主题是 Base.Theme.AppCompat.Light.Dialog.FixedSize )。这是我的 styles.xml

Step 1: create two custom themes for your application in styles.xml. One for normal activities, and another for dialog activities. It is important for the custom dialog theme to inherit from a base theme that is also a dialog. In my case, the parent theme is Base.Theme.AppCompat.Light.Dialog.FixedSize). Here is my styles.xml:

<resources>

    <!-- custom normal activity theme -->    
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    </style>

    <!-- custom dialog activity theme -->
    <style name="AppTheme.Dialog" parent="Base.Theme.AppCompat.Light.Dialog.FixedSize">
        <!-- removing the dialog's action bar -->
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

</resources>

AndroidManifest.xml中的第2步: ,将有问题的 Activity 的主题设置为任何对话框主题。这使得android os认为 Activity 是一个对话框,所以它会在它后面绘制 Activity ,而不是把它弄黑了。就我而言,我使用 Theme.AppCompat.Dialog 。下面是我的 AndroidManifest.xml

Step 2: in the AndroidManifest.xml, set the theme of the Activity in question to any dialog theme. This makes the android os think that the Activity is a dialog, so it will draw the Activity behind it, and not black it out. In my case, I used Theme.AppCompat.Dialog. Below is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.eric.questiondialog_artifact">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name=".DialogActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.Dialog"> <-- IMPORTANT!!! -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

步骤3:在实际活动中,将主题以编程方式设置为正常活动的主题,或对话框的主题。我的 DialogActivity.java 如下:

Step 3: in the actual activity, set the theme programatically to either the theme for normal activities, or the theme for dialogs. My DialogActivity.java is below:

package com.example.eric.questiondialog_artifact;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class DialogActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        setTheme(R.style.AppTheme_Dialog); // can either use R.style.AppTheme_Dialog or R.style.AppTheme as deined in styles.xml
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
    }
}

这篇关于如何以编程方式将活动主题化为对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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