How do I create a popup overlay view in an activit

2019-07-16 12:07发布

问题:

I'd like to show when pressed a button a popup onto my Activity. I was inspired by this question

So I use the "merge" control in the content xml of the activity and put in it the 2 different Layouts, the problem occurs obviously at this line (code taken from the question linked above):

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

because ofc FragmentManager works for Fragments.

My issue is that my Activity is not "fragmented", but its LinearLayout is inflated directly in the Activity and not in a Fragment in the Activity.

Can I get a similar effect like that question in an Activity or shall I forcibly embed all its controls in a Fragment?

Thanks in advance

回答1:

Ok so this might seem like a lot of code, but it's really easy.

First, you will create the dialog layout you want in XML. (not in the same XML as the activity view) Here's an example.

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>

Then, in your Activity do the following:

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();
}

And finally, in onCreate of your activity, call that method

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

Hope this helps!