使 Android Activity 看起来像对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2394477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:41:25  来源:igfitidea点击:

Make Android Activity looks like dialog

androiduser-interfaceandroid-activity

提问by Matroska

I am trying to open a dialog on widget click. I have solved the problem skinning the activity started on click with android:theme="@android:style/Theme.Dialog". Unfortunately I cannot reach the same look of a dialog.

我正在尝试在单击小部件时打开一个对话框。我已经解决了通过单击开始的活动皮肤的问题android:theme="@android:style/Theme.Dialog"。不幸的是,我无法达到对话框的相同外观。

This is the outcome:

这是结果:

Dialog from widget

来自小部件的对话框

Instead, I would like to reach this result (except for the button, of course):

相反,我想达到这个结果(当然按钮除外):

Desired dialog from widget

来自小部件的所需对话框

(the widget dialog you can see keeping the screen pushed)

(您可以看到保持屏幕推送的小部件对话框)

As you can see there are some differences: the color of the list items, the color of the text and the list item separator. Is there a predefined theme/style to obtain the same look of a standard dialog? If not, what are the steps to follow to reach that result?

如您所见,存在一些差异:列表项的颜色、文本的颜色和列表项分隔符。是否有预定义的主题/样式来获得与标准对话框相同的外观?如果不是,要遵循哪些步骤才能达到该结果?

I have seen that the widget provided by FoxyRing has the behaviour I would like to have.

我已经看到 FoxyRing 提供的小部件具有我想要的行为。

回答by Hubert

Why not use a "traditional" Main Activity with a transparent background layout

为什么不使用具有透明背景布局的“传统”主要活动

and call a standard dialog from it ?

并从中调用标准对话框?

... well if I understood you correctly, that would make the trick in a very easy way, isn't it ?

……好吧,如果我理解正确的话,那会很容易搞定,不是吗?

回答by Nick

you could dynamically create the dialog like this:

您可以像这样动态创建对话框:

        Context mContext = this;
        Dialog dialog = new Dialog(mContext);

        dialog.setContentView(R.layout.data_dialog);
        dialog.setTitle("Your title");

        AlertDialog.Builder builder;
        AlertDialog alertDialog;

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.data_dialog,
                (ViewGroup) findViewById(R.id.AbsoluteLayout01));

        builder = new AlertDialog.Builder(mContext);
        builder.setView(layout);
        alertDialog = builder.create();
        alertDialog.show();

        final EditText txtUsername = (EditText) layout.findViewById(R.id.txtUsername);

        final AlertDialog thisDialog = alertDialog;

         Button btnSave = (Button) layout.findViewById(R.id.btnSave);
         btnSave.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {

                String strtxtUsername = txtUsername.getText().toString();

                //do something...

             }
         });                

To close the dialog, call thisDialog.dismiss();

要关闭对话框,请调用 thisDialog.dismiss();

The style looks like the regular Theme.Light.NoTitleBar with a ListView with an Icon and a Title-Text.

样式看起来像常规的 Theme.Light.NoTitleBar,带有一个带有图标和标题文本的 ListView。

Hope that helps!

希望有帮助!

回答by Nick

There is actually a far simpler way to do this than what I suggested below.
Have a look at: https://developer.android.com/guide/topics/ui/dialogs.htmlunder the headline "Adding a list".

实际上有一种比我在下面建议的方法更简单的方法。
看看:https: //developer.android.com/guide/topics/ui/dialogs.html在标题“添加列表”下。