如何在没有片段的活动中创建弹出式叠加视图? [英] How do I create a popup overlay view in an activity without Fragment?

查看:82
本文介绍了如何在没有片段的活动中创建弹出式叠加视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在按下按钮时向我的活动显示一个弹出窗口.我受到这个问题的启发这个问题

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

因此,我在活动的内容xml中使用了合并"控件,​​并在其中放入了2种不同的布局,问题显然在此行发生(从上面链接的问题中获取的代码):

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

因为ofc FragmentManager适用于片段.

because ofc FragmentManager works for Fragments.

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

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.

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

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

预先感谢

推荐答案

好吧,这看起来像很多代码,但这确实很容易.

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

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

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

最后,在您的活动的onCreate中,调用该方法

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

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

希望这会有所帮助!

这篇关于如何在没有片段的活动中创建弹出式叠加视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆