簡體   English   中英

在Android應用程序中實現彈出窗口的最佳方法

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

我有一個主要活動,我有幾個菜單按鈕。 當用戶按下特定按鈕時,我想打開一個新activity ,該activity還有一些我需要處理其點擊的按鈕。 換句話說,我需要將窗口功能彈出為正常活動。
我在線查看並找到了幾種實現方法,例如:只需自定義activity的大小,在清單中使用diaglog主題,將其用作fragment或使用Popup Window類。 但由於我是android的新手,我希望以最好的方式為我的項目實現它。
有人可以幫我實現嗎?

編輯:這是我想在彈出窗口中使用的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>

創建方法,你想在你的活動中打開彈出windiw像這樣,這里p是你想要完全打開你的窗口的特定點(位置)。

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

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

            }
        });

然后,

@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

 <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>

取決於您的需求。
如果您只需要顯示“經典”彈出窗口(輸入字段或小顏色選擇器時輸入錯誤) - 請使用PopupWindow類。 你可以用我的要點
如果您的消息更通用(例如“沒有互聯網連接”)或用戶應選擇是/否 - 請使用對話框。 很酷的圖書館
很少使用具有自定義大小的活動。 也許在某些撥號應用程序中 因此,選擇實施取決於您的需求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM