Android PopupWindow(懸浮框)的基本使用

2023-03-31 13:43 更新

本節(jié)引言:

本節(jié)給大家?guī)淼氖亲詈笠粋€用于顯示信息的UI控件——PopupWindow(懸浮框),如果你想知道 他長什么樣子,你可以打開你手機的QQ,長按列表中的某項,這個時候后彈出一個黑色的小 對話框,這種就是PopupWindow了,和AlertDialog對話框不同的是,他的位置可以是隨意的;

另外AlertDialog是非堵塞線程的,而PopupWindow則是堵塞線程的!而官方有這樣一句話來介紹 PopupWindow:

A popup window that can be used to display an arbitrary view. The popup window is

a floating container that appears on top of the current activity.

大概意思是:一個彈出窗口控件,可以用來顯示任意View,而且會浮動在當(dāng)前activity的頂部

下面我們就來對這個控件進(jìn)行學(xué)習(xí)~

官方文檔:PopupWindow


1.相關(guān)方法解讀


1)幾個常用的構(gòu)造方法

我們在文檔中可以看到,提供給我們的PopupWindow的構(gòu)造方法有九種之多,這里只貼實際 開發(fā)中用得較多的幾個構(gòu)造方法:

  • public PopupWindow (Context context)
  • public PopupWindow(View contentView, int width, int height)
  • public PopupWindow(View contentView)
  • public PopupWindow(View contentView, int width, int height, boolean focusable)

參數(shù)就不用多解釋了吧,contentView是PopupWindow顯示的View,focusable是否顯示焦點


2)常用的一些方法

下面介紹幾個用得較多的一些方法,其他的可自行查閱文檔:

  • setContentView(View contentView):設(shè)置PopupWindow顯示的View
  • getContentView():獲得PopupWindow顯示的View
  • showAsDropDown(View anchor):相對某個控件的位置(正左下方),無偏移
  • showAsDropDown(View anchor, int xoff, int yoff):相對某個控件的位置,有偏移
  • showAtLocation(View parent, int gravity, int x, int y): 相對于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設(shè)置偏移或無偏移 PS:parent這個參數(shù)只要是activity中的view就可以了!
  • setWidth/setHeight:設(shè)置寬高,也可以在構(gòu)造方法那里指定好寬高, 除了可以寫具體的值,還可以用WRAP_CONTENT或MATCH_PARENT, popupWindow的width和height屬性直接和第一層View相對應(yīng)。
  • setFocusable(true):設(shè)置焦點,PopupWindow彈出后,所有的觸屏和物理按鍵都由PopupWindows 處理。其他任何事件的響應(yīng)都必須發(fā)生在PopupWindow消失之后,(home 等系統(tǒng)層面的事件除外)。 比如這樣一個PopupWindow出現(xiàn)的時候,按back鍵首先是讓PopupWindow消失,第二次按才是退出 activity,準(zhǔn)確的說是想退出activity你得首先讓PopupWindow消失,因為不并是任何情況下按back PopupWindow都會消失,必須在PopupWindow設(shè)置了背景的情況下 。
  • setAnimationStyle(int):設(shè)置動畫效果

2.使用代碼示例

運行效果圖

實現(xiàn)關(guān)鍵代碼

先貼下動畫文件:anim_pop.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="0"
        android:toAlpha="1"
        android:duration="2000">
    </alpha>
</set> 

接著是popupWindow的布局:item_popip.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="@drawable/ic_pop_bg"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_xixi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="嘻嘻"
        android:textSize="18sp" />

    <Button
        android:id="@+id/btn_hehe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="呵呵"
        android:textSize="18sp" />

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Button btn_show;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        btn_show = (Button) findViewById(R.id.btn_show);
        btn_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initPopWindow(v);
            }
        });
    }

    private void initPopWindow(View v) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_popup, null, false);
        Button btn_xixi = (Button) view.findViewById(R.id.btn_xixi);
        Button btn_hehe = (Button) view.findViewById(R.id.btn_hehe);
        //1.構(gòu)造一個PopupWindow,參數(shù)依次是加載的View,寬高
        final PopupWindow popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popWindow.setAnimationStyle(R.anim.anim_pop);  //設(shè)置加載動畫

        //這些為了點擊非PopupWindow區(qū)域,PopupWindow會消失的,如果沒有下面的
        //代碼的話,你會發(fā)現(xiàn),當(dāng)你把PopupWindow顯示出來了,無論你按多少次后退鍵
        //PopupWindow并不會關(guān)閉,而且退不出程序,加上下述代碼可以解決這個問題
        popWindow.setTouchable(true);
        popWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
                // 這里如果返回true的話,touch事件將被攔截
                // 攔截后 PopupWindow的onTouchEvent不被調(diào)用,這樣點擊外部區(qū)域無法dismiss
            }
        });
        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要為popWindow設(shè)置一個背景才有效

        //設(shè)置popupWindow顯示的位置,參數(shù)依次是參照View,x軸的偏移量,y軸的偏移量
        popWindow.showAsDropDown(v, 50, 0);

        //設(shè)置popupWindow里的按鈕的事件
        btn_xixi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "你點擊了嘻嘻~", Toast.LENGTH_SHORT).show();
            }
        });
        btn_hehe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "你點擊了呵呵~", Toast.LENGTH_SHORT).show();
                popWindow.dismiss();
            }
        });
    }
}

3.示例代碼下載

PopWindowDemo.zip


本節(jié)小結(jié):

時間關(guān)系,并沒有想到好一點的示例,就寫了一個簡單的demo,應(yīng)該能滿足簡單的需要,另外 如果想深入研究下PopupWindow的話可看下述的參考文獻(xiàn):
Android PopupWindow的使用和分析
Android PopupWindow詳解
嗯,就說這么多,謝謝~


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號