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

Bla*_*Box 2 android overlay android-layout android-fragments android-activity

我想在按下按钮弹出一个按钮时显示我的Activity.我受到了这个问题的启发

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

FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
    .add(R.id.overlay_fragment_container, yourFragment)
    .commit();
Run Code Online (Sandbox Code Playgroud)

因为ofc FragmentManager适用于Fragments.

我的问题是我的Activity没有"碎片化",但它的LinearLayout直接在Activity中膨胀,而不是在Activity中的Fragment中膨胀.

我是否可以在活动中获得类似该问题的效果,还是应该将其所有控件强制嵌入片段中?

提前致谢

ᴛʜᴇ*_*ᴛᴇʟ 6

好的,这可能看起来像很多代码,但它真的很容易.

首先,您将在XML中创建所需的对话框布局.(与活动视图不在同一个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>
Run Code Online (Sandbox Code Playgroud)

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

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();
}
Run Code Online (Sandbox Code Playgroud)

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

myButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        showMyDialog(context);
    }
});
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!