Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android PopupWindow 懸浮窗口的入門實戰

編輯:關於 Android 開發

快速上手 PopupWindow 懸浮容器

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)

編寫彈窗布局 popu_layout.xml

使用 GridLayout 排布一排銀行圖標,簡單直觀:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="https://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 核心邏輯

package com.example.wgh.popupwindow;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.PopupWindow;

public class MainActivity extends AppCompatActivity {
 private View mPopView = null;
 private Button showPopupWindow = null;
 private Button dismissPopupWindow = null;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 initView();
 showPopupWindow.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 showPopupWindow();
 }
 });
 }

 private void initView() {
 showPopupWindow = (Button) findViewById(R.id.showPopupWindow);
 dismissPopupWindow = (Button) findViewById(R.id.dismissPopupWindow);
 /** 實例化 popupWindow 要添加的布局 */
 mPopView = LayoutInflater.from(this).inflate(R.layout.popu_layout, null);
 }

 private void showPopupWindow() {
 /** 實例化 popupWindow 對象 */
 PopupWindow popupWindow = new PopupWindow(mPopView, GridLayout.LayoutParams.MATCH_PARENT, GridLayout.LayoutParams.WRAP_CONTENT);
 // 設置 popupWindow 中的 item 可以被點擊,這句是必須的
 popupWindow.setFocusable(true);
 // 設置 PopupWindow 的背景
 // 若不設置背景,點擊外部區域或按 Back 鍵都無法 dismiss 掉 popupWindow
 ColorDrawable dw = new ColorDrawable(0xb0000000);
 popupWindow.setBackgroundDrawable(dw);
 // 設置 popupWindow 顯示的位置
 popupWindow.showAtLocation(showPopupWindow, Gravity.BOTTOM, 0, 200);
 }
}

關於顯示位置的兩種定位方式

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

// 相對某個控件的位置,有偏移,xoff 為 X 軸偏移量,yoff 為 Y 軸偏移量
popupWindow.showAsDropDown(View anchor, int xoff, int yoff)

// 在父容器的指定位置,gravity 為相對位置,
// 如:正中央 Gravity.CENTER、下方 Gravity.BOTTOM、右下方 Gravity.RIGHT|Gravity.BOTTOM,
// 後兩個參數為 x / y 軸的偏移量
popupWindow.showAtLocation(View parent, int gravity, int x, int y)

小貼士:有興趣的朋友還可以為 PopupWindow 加上進出動畫,讓彈出過程不再生硬。若有理解上的偏差,也歡迎大家指正交流。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved