Android widget之PopupWindow

概述

Android應(yīng)用中經(jīng)常會(huì)彈出一個(gè)窗體,進(jìn)行一些操作,比如說分享、選擇城市等等,類似于AlertDialog,下面將詳細(xì)講解PopupWindow。
構(gòu)造方法


PopupWindow的構(gòu)造方法,官方給出的有9種,項(xiàng)目中常用的只有最后兩種

  1. PopupWindow(Context context)
    Create a new empty, non focusable popup window of dimension (0,0).
    創(chuàng)建一個(gè)寬度為0、高度為0的無焦點(diǎn)的空彈窗。
    這種方法創(chuàng)建的彈窗,提供了背景
    context為上下文。

  2. PopupWindow(Context context, AttributeSet attrs)
    Create a new empty, non focusable popup window of dimension (0,0).
    創(chuàng)建一個(gè)寬度為0、高度為0的無焦點(diǎn)的空彈窗。
    這種方法創(chuàng)建的彈窗,提供了背景

  3. PopupWindow(Context context, AttributeSet attrs, int defStyleAttr)
    Create a new empty, non focusable popup window of dimension (0,0).
    創(chuàng)建一個(gè)寬度為0、高度為0的無焦點(diǎn)的空彈窗。
    這種方法創(chuàng)建的彈窗,提供了背景

  4. PopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
    Create a new empty, non focusable popup window of dimension (0,0).
    創(chuàng)建一個(gè)寬度為0、高度為0的無焦點(diǎn)的空彈窗。
    這種方法創(chuàng)建的彈窗,提供了背景

  5. PopupWindow()
    Create a new empty, non focusable popup window of dimension (0,0).
    The popup does not provide any background. This should be handled by the content view.
    創(chuàng)建一個(gè)寬度為0、高度為0的無焦點(diǎn)的空彈窗。
    這種方法創(chuàng)建的彈窗,沒有背景,需要自己填充

  6. PopupWindow(View contentView)
    Create a new non focusable popup window which can display the contentView. The dimension of the window are (0,0).
    The popup does not provide any background. This should be handled by the content view.
    創(chuàng)建一個(gè)寬度為0、高度為0,可以自定義內(nèi)容的無焦點(diǎn)彈窗。
    這種方法創(chuàng)建的彈窗沒有背景,需要自己填充
    contentView:彈窗展示的內(nèi)容

  7. PopupWindow(int width, int height)
    Create a new empty, non focusable popup window. The dimension of the window must be passed to this constructor.
    The popup does not provide any background. This should be handled by the content view.
    創(chuàng)建一個(gè)寬度為width、高度為height的無焦點(diǎn)空彈窗。
    這種方法創(chuàng)建的彈窗沒有背景,需要自己填充
    height:彈窗的高度
    width:彈窗的寬度

  8. PopupWindow(View contentView, int width, int height)
    Create a new non focusable popup window which can display the contentView.
    創(chuàng)建一個(gè)自定義寬度、高度、顯示內(nèi)容的無焦點(diǎn)彈窗。
    contentView:自定義的彈窗顯示內(nèi)容
    width:彈窗寬度
    height:彈窗高度
    舉個(gè)栗子

View view=getLayoutInflater().inflate(R.layout.window, null);
PopupWindow  mPop =new  
PopupWindow(view,LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
  1. PopupWindow(View contentView, int width, int height, boolean focusable)
    Create a new popup window which can display the contentView.
    創(chuàng)建一個(gè)自定義寬度、高度、顯示內(nèi)容、是否有焦點(diǎn)的彈窗。
    contentView:自定義的彈窗顯示內(nèi)容
    width:彈窗寬度
    height:彈窗高度
    focusable:是否有焦點(diǎn),true:有焦點(diǎn);false:無焦點(diǎn)(默認(rèn)為false,與第8中構(gòu)造方法效果相同)
    舉個(gè)栗子
View view=getLayoutInflater().inflate(R.layout.window, null);
PopupWindow  mPop =new  
PopupWindow(view,LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,true);

顯示方式

PopupWindow一般有兩種展示方法,用showAsDropDown()和showAtLocation()兩種方法實(shí)現(xiàn)。一般參數(shù)有兩種,有偏移和無偏移。
官方的方法如下
showAsDropDown()

  • showAsDropDown(View anchor, int xoff, int yoff, int gravity)
  • showAsDropDown(View anchor, int xoff, int yoff)
  • showAsDropDown(View anchor)
    anchor可以理解為錨,彈窗顯示在anchor下面,xoff為X軸偏移量,yoff為Y軸偏移量,gravity為顯示方式
    上栗子
  • mPop.showAsDropDown(anchor);
    彈窗顯示在anchor正下方,無任何偏移。
  • mPop.showAsDropDown(anchor,xoff,yoff);
    彈窗在anchor下方顯示,X軸偏移xoff,Y軸偏移yoff。
  • mPop.showAsDropDown(anchor,xoff,yoff,Gravity.CENTER);
    彈出在anchor下方居中顯示,同時(shí)X軸偏移xoff,Y軸偏移yoff。

showAtLocation()

  • showAtLocation(View parent, int gravity, int x, int y)
    按函數(shù)名來理解,很容易看出該函數(shù)的意義,即在某個(gè)位置顯示彈窗,我們要做的就是為彈窗設(shè)置Location。
    parent:彈窗顯示的父容器
    gravity:顯示方式
    x:X軸偏移量
    y:Y軸偏移量
    栗子來啦
mPop.showAtLocation(PopWindow.this.findViewById(R.id.rl), 
Gravity.TOP | Gravity.LEFT, 20, 20);

在屏幕頂部|居右,X軸偏移20,Y軸偏移20;

實(shí)栗

這里實(shí)現(xiàn)一個(gè)常見的分享彈窗,點(diǎn)擊彈窗外面或者點(diǎn)擊back鍵,關(guān)閉彈窗


這里寫圖片描述

這里寫圖片描述
這里寫圖片描述

兩個(gè)布局文件,一個(gè)主界面布局文件、一個(gè)彈框內(nèi)容布局文件
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="showAsDropDown無偏移"  />
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="showAsDropDown有偏移"  />

</LinearLayout>

popup_share.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:background="@color/white">
    <LinearLayout 
        android:layout_marginLeft="10dp"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/umeng_socialize_wechat"/>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10sp"
            android:textColor="@color/black"
            android:layout_gravity="center"
            android:text="微信好友"/>
    </LinearLayout>
    <LinearLayout 
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/umeng_socialize_wxcircle"/>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10sp"
            android:textColor="@color/black"
            android:layout_gravity="center"
            android:text="微信朋友圈"/>
    </LinearLayout>
    <LinearLayout 
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/umeng_socialize_qq_on"/>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10sp"
            android:textColor="@color/black"
            android:layout_gravity="center"
            android:text="QQ好友"/>
    </LinearLayout>
    <LinearLayout 
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/umeng_socialize_qzone_on"/>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10sp"
            android:textColor="@color/black"
            android:layout_gravity="center"
            android:text="QQ空間"/>
    </LinearLayout>
    <LinearLayout 
        android:layout_marginRight="10dp"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/umeng_socialize_sina_on"/>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10sp"
            android:textColor="@color/black"
            android:layout_gravity="center"
            android:text="微博"/>
    </LinearLayout>
</LinearLayout>

重新activity的onCreate方法

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        popupView= getLayoutInflater().inflate(R.layout.popup_share, null);
        mPop = new PopupWindow(popupView,
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,true);
        mPop.setBackgroundDrawable(new ColorDrawable(0));
        
        btn1=(Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(this);
        btn2=(Button) findViewById(R.id.btn2);
        btn2.setOnClickListener(this);
    }

兩種顯示方法的觸發(fā)事件

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btn1:
            mPop.showAsDropDown(btn1);
            break;
        case R.id.btn2:
            mPop.showAsDropDown(btn2,290,-50);
            break;
        default:
            break;
        }
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容