Android—PopupWindow的簡單使用,androidpopupwindow
PopupWindow 是一個可以顯示在當前 Activity 之上的浮動容器,這個Demo要實現的功能是,點擊布局中的兩個按鈕,進而控制PopupWindow的顯示與消失,代碼中有詳細的注釋首先看一下效果展示:
在上代碼之前,先總結一下PopupWindow的用法:
1:執行個體化PopupWindow的對象,三個參數分別對應:填充的布局檔案、在當前Activity上所佔的寬、高PopupWindow popupWindow= new PopupWindow(contentView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);2:完成第一步所需要的布局檔案,並執行個體出來View contentView = mLayoutInflater.inflate(R.layout.pop, null)3:設定PopupWindow 所必備的兩個屬性 //popupWindow的背景 (1)popupWindow.setBackgroundDrawable(......); //popupWindow要顯示的位置 (2)popupWindow.showAtLocation(View parent, int gravity, int x, int y)
接下來,上代碼!popupWindow所要添加的布局檔案:popu_layout.xml
<?xml version="1.0" encoding="utf-8"?><GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#87cdff" android:columnCount="4" android:orientation="horizontal" android:rowCount="4"> <ImageView android:layout_width="65dp" android:layout_height="65dp" android:layout_margin="12dp" android:background="@drawable/icon_bank_gongshang" /> <ImageView android:layout_width="65dp" android:layout_height="65dp" android:layout_margin="12dp" android:background="@drawable/icon_bank_guangda" /> <ImageView android:layout_width="65dp" android:layout_height="65dp" android:layout_margin="12dp" android:background="@drawable/icon_bank_jianhang" /> <ImageView android:layout_width="65dp" android:layout_height="65dp" android:layout_margin="12dp" android:background="@drawable/icon_bank_jiaotong" /> <ImageView android:layout_width="65dp" android:layout_height="65dp" android:layout_margin="12dp" android:background="@drawable/icon_bank_minsheng" /> <ImageView android:layout_width="65dp" android:layout_height="65dp" android:layout_margin="12dp" android:background="@drawable/icon_bank_nongye" /> <ImageView android:layout_width="65dp" android:layout_height="65dp" android:layout_margin="12dp" android:background="@drawable/icon_bank_gongshang" /> <ImageView android:layout_width="65dp" android:layout_height="65dp" android:layout_margin="12dp" android:background="@drawable/icon_bank_pingan" /> <ImageView android:layout_width="65dp" android:layout_height="65dp" android:layout_margin="12dp" android:background="@drawable/icon_bank_zhaoshang" /> <ImageView android:layout_width="65dp" android:layout_height="65dp" android:layout_margin="12dp" android:background="@drawable/icon_bank_youzheng" /> <ImageView android:layout_width="65dp" android:layout_height="65dp" android:layout_margin="12dp" android:background="@drawable/icon_bank_xingye" /> <ImageView android:layout_width="65dp" android:layout_height="65dp" android:layout_margin="12dp" android:background="@drawable/icon_bank_pufa" /></GridLayout>
相當簡單的布局,做出來就是這麼一個玩意:
MainActivity:
1 package com.example.wgh.popupwindow; 2 import android.graphics.drawable.ColorDrawable; 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.Gravity; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.widget.Button; 9 import android.widget.GridLayout;10 import android.widget.PopupWindow;11 12 public class MainActivity extends AppCompatActivity {13 14 private View mPopView = null;15 private Button showPopupWindow = null;16 private Button dismissPopupWindow = null;17 @Override18 protected void onCreate(Bundle savedInstanceState) {19 super.onCreate(savedInstanceState);20 setContentView(R.layout.activity_main);21 22 initView();23 showPopupWindow.setOnClickListener(new View.OnClickListener() {24 @Override25 public void onClick(View view) {26 showPopupWindow();27 }28 });29 }30 31 private void initView() {32 showPopupWindow = (Button) findViewById(R.id.showPopupWindow);33 dismissPopupWindow = (Button) findViewById(R.id.dismissPopupWindow);34 /**35 * 執行個體popupWindow要添加的布局36 */37 mPopView = LayoutInflater.from(this).inflate(R.layout.popu_layout, null);38 }39 40 private void showPopupWindow() {41 /**42 * 執行個體popupWindow對象43 */44 PopupWindow popupWindow = new PopupWindow(mPopView, GridLayout.LayoutParams.MATCH_PARENT, GridLayout.LayoutParams.WRAP_CONTENT);45 //設定popupWindow中的item可以被點擊,這句話是必須要添加的46 popupWindow.setFocusable(true);47 //設定PopupWindow的背景48 //如果不設定背景,會導致無論是點擊外部地區還是Back鍵都無法dismiss掉popupWindow49 ColorDrawable dw = new ColorDrawable(0xb0000000);50 popupWindow.setBackgroundDrawable(dw);51 //設定popupWindow顯示的位置52 popupWindow.showAtLocation(showPopupWindow, Gravity.BOTTOM,0,200);53 }54 }
最後說一下關於popupWindow顯示位置的屬性設定
1 // 相對某個控制項的位置(正左下方),無位移2 popupWindow.showAsDropDown(View anchor) 3 // 相對某個控制項的位置,有位移,xoff 為 X 軸的位移量,yoff 為 Y 軸的位移量 4 popupWindow.showAsDropDown(View anchor, int xoff, int yoff)5 // 在父容器的什麼位置,gravity 為相對位置,6 //如:正中央 Gravity.CENTER、下方 Gravity.BOTTOM、Gravity.RIGHT|Gravity.BOTTOM 右下方等,後面兩個參數為 x/y 軸的位移量。7 popupWindow.showAtLocation(View parent, int gravity, int x, int y)
有興趣的童鞋可以為popupWindow設定上動畫,這樣在彈出的時候,不會顯得那麼突兀,哈哈
如果有什麼地方是錯誤的,請大家批評指正。