简体   繁体   English

在Android应用程序中实现弹出窗口的最佳方法

[英]best way to implement pop up window in android application

I have the main activity in which I have few menu buttons. 我有一个主要活动,我有几个菜单按钮。 When the user presses specific button I want to open a new activity which also have a couple of buttons which I need to handle their click. 当用户按下特定按钮时,我想打开一个新activity ,该activity还有一些我需要处理其点击的按钮。 In other words, I need to pop up window functionality as normal activity. 换句话说,我需要将窗口功能弹出为正常活动。
I looked online and found several ways to implement this such as: just customising the size of the activity , use diaglog theme in the manifest, use it as a fragment or use Popup Window Class. 我在线查看并找到了几种实现方法,例如:只需自定义activity的大小,在清单中使用diaglog主题,将其用作fragment或使用Popup Window类。 But as I am new to android I want the best way to implement it for my project. 但由于我是android的新手,我希望以最好的方式为我的项目实现它。
Can someone help me achieve this? 有人可以帮我实现吗?

EDIT: this is the xml file i want to use in the pop up window (for better explanation of what i want to achieve): 编辑:这是我想在弹出窗口中使用的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:gravity="center_horizontal"
    android:orientation="vertical"
    android:background="#0091cb"
    android:padding="16dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="@dimen/activity_horizontal_margin"
        android:weightSum="3"
        android:id="@+id/check">

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/computer"
            android:paddingTop="12dp"
            android:layout_marginLeft="10dp"
            android:text="button1"
            android:textSize="10dp"
            android:textColor="#fff"
            android:layout_weight="1"
            android:onClick="button1_OnClick"/>

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/electrical"
            android:paddingTop="12dp"
            android:layout_marginLeft="10dp"
            android:text="button2"
            android:textSize="10dp"
            android:textColor="#fff"
            android:layout_weight="1"
            android:onClick="button2_OnClick"/>

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/hdtv"
            android:paddingTop="12dp"
            android:layout_marginLeft="10dp"
            android:text="button3"
            android:textSize="10dp"
            android:textColor="#fff"
            android:layout_weight="1"
            android:onClick="button3_OnClick"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:orientation="horizontal"
        android:layout_below="@id/check"
        android:paddingTop="10dp">

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/bill"
            android:paddingTop="12dp"
            android:layout_marginLeft="10dp"
            android:text="button4"
            android:textSize="10dp"
            android:textColor="#fff" 
            android:onClick="button4_OnClick"/>

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/water"
            android:paddingTop="12dp"
            android:text="button5"
            android:textSize="10dp"
            android:textColor="#fff"
            android:onClick="button5_OnClick" />

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/electrical"
            android:paddingTop="12dp"
            android:text="button6"
            android:textSize="10dp"
            android:textColor="#fff" 
            android:onClick="button6_OnClick" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:orientation="horizontal">

        <Button
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/notepad"
            android:paddingTop="7dp"
            android:text="button7"
            android:textSize="10dp"
            android:textColor="#fff"
            android:onClick="button7_OnClick" />
        </LinearLayout>
</LinearLayout>

create method where u want to open popup windiw in your activity Like this,here p is a specific point(location) where you want exactly open your window. 创建方法,你想在你的活动中打开弹出windiw像这样,这里p是你想要完全打开你的窗口的特定点(位置)。

  final Point p = new Point();
  show_popup.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showpopupwindows(Activity, p);

            }
        });

then, 然后,

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    int location[] = new int[2];

    show_popup.getLocationOnScreen(location);
    p.x = location[0];
    p.y = location[1];
}

private void showpopupwindows(final Activity context, Point p) {

    LinearLayout viewGroup = (LinearLayout) context
            .findViewById(R.id.popup_menu);

    LayoutInflater layoutInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    Display display = context.getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);

    int popupwidth = (size.x / 2);
    int popupheight =(size.y / 2);

    View layout = layoutInflater.inflate(R.layout.detail_pop, viewGroup);

    final PopupWindow popup = new PopupWindow(context);

    popup.setContentView(layout);
    popup.setWidth(popupwidth);
    popup.setHeight(popupheight);
    popup.setFocusable(true);
    popup.setAnimationStyle(R.style.WindowAnimation);
    popup.setBackgroundDrawable(new ColorDrawable(
            android.graphics.Color.TRANSPARENT));

    popup.showAtLocation(layout, Gravity.NO_GRAVITY, popupwidth,popupheight);

    detail_pop1 = (TextView) layout.findViewById(R.id.detail_pop1);

    detail_pop1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getContext(), "pop window is opened, Toast.LENGTH_SHORT).show();

        }
    });
}

detail_pop.xml detail_pop.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup_menu"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:background="@color/skyblue"
    android:gravity="center"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/detail_pop1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:drawableTop="@drawable/ic_launcher"
        android:gravity="center"
        android:text="P"
        android:textSize="@dimen/font_24" />


</LinearLayout>

Depends on your needs. 取决于您的需求。
If you need just to show 'classic' popup window (error while typing in input field or small color picker) - use PopupWindow class. 如果您只需要显示“经典”弹出窗口(输入字段或小颜色选择器时输入错误) - 请使用PopupWindow类。 You can use my gist . 你可以用我的要点
If your message is more general (eg 'There is no internet connection') or user should choose yes/no - use dialog. 如果您的消息更通用(例如“没有互联网连接”)或用户应选择是/否 - 请使用对话框。 There is cool library . 很酷的图书馆
Activity with custom size is used rarely. 很少使用具有自定义大小的活动。 Maybe in some dial applications. 也许在某些拨号应用程序中 So choose implementation depends on your needs. 因此,选择实施取决于您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM