如何在没有 Fragment 的 activity 中创建弹出覆盖视图?

How do I create a popup overlay view in an activity without Fragment?

我想在按下按钮时在 Activity 上显示一个弹出窗口。我受到启发 by this question.

所以我在 activity 的内容 xml 中使用了“合并”控件,​​并在其中放入了 2 个不同的布局,问题显然出现在这一行(代码取自问题上面链接):

FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
    .add(R.id.overlay_fragment_container, yourFragment)
    .commit();

当然,FragmentManager 适用于 Fragments

我的问题是我的 Activity 没有“碎片化”。 LinearLayout直接在Activity中膨胀,而不是在Activity.Fragment中膨胀。

我可以在 Activity 中获得与该问题类似的效果,还是我应该将其所有控件强行嵌入到 Fragment 中?

提前致谢

好的,这可能看起来像很多代码,但它真的很简单。

首先,您将在 XML 中创建所需的对话框布局。 (与 activity 视图不在同一个 XML 中)这是一个示例。

custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:text="New Text"
        android:id="@+id/txtTitle" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_weight="1" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="52dp">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/btnBtmLeft"
            android:layout_weight="1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/btnBtmRight"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

然后,在您的 Activity 中执行以下操作:

private void showMyDialog(Context context) {
    final Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.custom_dialog);
    dialog.setCanceledOnTouchOutside(false);
    dialog.setCancelable(true);

    TextView textView = (TextView) dialog.findViewById(R.id.txtTitle);
    ListView listView = (ListView) dialog.findViewById(R.id.listView);
    Button btnBtmLeft = (Button) dialog.findViewById(R.id.btnBtmLeft);
    Button btnBtmRight = (Button) dialog.findViewById(R.id.btnBtmRight);

    btnBtmLeft.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    btnBtmRight.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // do whatever you want here
        }
    });

    /**
     * if you want the dialog to be specific size, do the following
     * this will cover 85% of the screen (85% width and 85% height)
     */
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    int dialogWidth = (int)(displayMetrics.widthPixels * 0.85);
    int dialogHeight = (int)(displayMetrics.heightPixels * 0.85);
    dialog.getWindow().setLayout(dialogWidth, dialogHeight);

    dialog.show();
}

最后,在 activity 的 onCreate 中调用该方法

myButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        showMyDialog(context);
    }
});

希望对您有所帮助!