1. 程式人生 > >popupWindow詳解和仿微信彈框例項

popupWindow詳解和仿微信彈框例項

1、介紹

(1)使用PopupWindow可實現彈出視窗效果,,其實和AlertDialog一樣,也是一種對話方塊,兩者也經常混用,但是也各有特點。

AlertDialog是非阻塞式對話方塊:AlertDialog彈出時,後臺還可以做事情;而PopupWindow是阻塞式對話方塊:PopupWindow彈出時,程式會等待。

(2)PopupWindow可以指定位置,並且彈出的框位置在指定位置的時可以用。當在背景色變色,在螢幕中間顯示時並且不能操作其它內容時使用AlertDialog。

2、PopupWindow屬性

(1)定義PopupWindow有如下四重方法,其中contentView(自己定義的彈出框佈局),width,height三個屬性必須設定,所以建議用方法四

//方法一:
public PopupWindow (Context context)
//方法二:
public PopupWindow(View contentView)
//方法三:
public PopupWindow(View contentView, int width, int height)
//方法四:
public PopupWindow(View contentView, int width, int height, boolean focusable)

(2)顯示彈出框位置

//相對某個控制元件的位置(正左下方),無偏移
showAsDropDown(View anchor):


//相對某個控制元件的位置,有偏移;xoff表示x軸的偏移,正值表示向左,負值表示向右;yoff表示相對y軸的偏移,正值是向下,負值是向上;
showAsDropDown(View anchor, int xoff, int yoff):
//相對於父控制元件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設定偏移或無偏移
showAtLocation(View parent, int gravity, int x, int y):

(3)其它函式

//關閉彈出框

public void dismiss()
//彈出框是否具有獲取焦點的能力,預設為false,就是如果彈出框中有Edittext時,並且想獲取焦點,則需要設定為true
public void setFocusable(boolean focusable)

//是否相應touch事件,預設為true,如果設定為false,則彈出框的所有點選按鈕都不可點選
public void setTouchable(boolean touchable)

//PopupWindow以外的區域是否可點選,即如果點選PopupWindow以外的區域,PopupWindow是否會消失。需要加入背景色才好使
public void setOutsideTouchable(boolean touchable)

//設定背景色的,只有設定了背景色,返回按鈕才會取消PopupWindow顯示,不然會返回上一個Activity
public void setBackgroundDrawable(Drawable background))

3、仿微信例項

(1)效果圖

 (2)程式碼首先需要寫一個xml檔案。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/btn1"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center_vertical"
        android:background="#393a3f"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="wrap_content"
            android:src="@drawable/icon_edit_delete"
            android:layout_marginLeft="15dp"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:text="發起群聊"
            android:textColor="#ffffff"
            android:layout_marginLeft="15dp"
            android:textSize="15sp"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:background="@color/gray_font"
        android:layout_height="1dp" />
    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center_vertical"
        android:background="#393a3f"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="wrap_content"
            android:src="@drawable/icon_edit_delete"
            android:layout_marginLeft="15dp"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:text="新增朋友"
            android:textColor="#ffffff"
            android:layout_marginLeft="15dp"
            android:textSize="15sp"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:background="@color/gray_font"
        android:layout_height="1dp" />
    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center_vertical"
        android:background="#393a3f"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="wrap_content"
            android:src="@drawable/icon_edit_delete"
            android:layout_marginLeft="15dp"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:text="掃一掃"
            android:textColor="#ffffff"
            android:layout_marginLeft="15dp"
            android:textSize="15sp"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:background="@color/gray_font"
        android:layout_height="1dp" />
    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center_vertical"
        android:background="#393a3f"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="wrap_content"
            android:src="@drawable/icon_edit_delete"
            android:layout_marginLeft="15dp"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:text="收復款"
            android:textColor="#ffffff"
            android:layout_marginLeft="15dp"
            android:textSize="15sp"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:background="@color/gray_font"
        android:layout_height="1dp" />
    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center_vertical"
        android:background="#393a3f"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="wrap_content"
            android:src="@drawable/icon_edit_delete"
            android:layout_marginLeft="15dp"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:text="幫助與反饋"
            android:textColor="#ffffff"
            android:layout_marginLeft="15dp"
            android:textSize="15sp"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:background="@color/gray_font"
        android:layout_height="1dp" />
</LinearLayout>

(3)activity程式碼

//顯示popupWindow
private fun showPopupWindow() {
    //獲取自定義選單的佈局檔案
    val popupWindow_view = layoutInflater.inflate(R.layout.add_rigth_layout, null, false)
    //建立popupWindow,設定寬度和高度
    popupWindow = PopupWindow(popupWindow_view, 200, 300, true)
    //內部控制元件的點選事件
    btn1!!.setOnClickListener(this)
    //設定選單的顯示位置
    popupWindow!!.showAsDropDown(right_btn, -150, 20)
    //相容5.0點選其他位置隱藏popupWindow
    popupWindow_view.setOnTouchListener(View.OnTouchListener { view, motionEvent ->
        //必須寫 v.performClick();解決與單擊事件的衝突
        when (motionEvent.action) {
            MotionEvent.ACTION_DOWN ->
                //如果選單不為空,且選單正在顯示
                if (popupWindow!!.isShowing()) {
                    popupWindow!!.dismiss()//隱藏選單
                    popupWindow = null//初始化選單
                }
            MotionEvent.ACTION_UP -> view.performClick()
            else -> {
            }
        }
        false
    })
}

(4)文章參考了:https://blog.csdn.net/harvic880925/article/details/49272285的內容,感覺他寫的更細一點,也有進入和退出動畫。