让Android Activity看起来像对话框

时间:2023-01-19 15:14:03

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提供的小部件具有我想要的行为。

3 个解决方案

#1


22  

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 ?

......好吧,如果我理解正确的话,这将以一种非常简单的方式制作技巧,不是吗?

#2


3  

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,带有带有Icon和Title-Text的ListView。

Hope that helps!

希望有所帮助!

#3


0  

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.html under the headline "Adding a list".

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

#1


22  

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 ?

......好吧,如果我理解正确的话,这将以一种非常简单的方式制作技巧,不是吗?

#2


3  

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,带有带有Icon和Title-Text的ListView。

Hope that helps!

希望有所帮助!

#3


0  

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.html under the headline "Adding a list".

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