編輯:關於 Android 開發
PopupWindow 是一個可以浮動顯示在當前 Activity 之上的容器,常用於製作菜單、快捷操作面板等效果。本文以一個小 Demo 為例,通過兩個按鈕來控制 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)
使用 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>
運行後就是下面這個樣子:

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 加上進出動畫,讓彈出過程不再生硬。若有理解上的偏差,也歡迎大家指正交流。
Android數據持久化三板斧:SharedPreferences、文件與SQLite详解
Android數據持久化三板斧:SharedPreferences、文件與SQLite详解 本文開篇先聚焦其中最輕量的一種——SharedPreferences。 一、使
Android 布局技巧:用 weightSum 與 layout_weight 讓按鈕精準居中並佔據一半寬度
一個屬性組合,解決「居中 + 佔半」的經典需求 本文原創地址:https://www.cnblogs.com/wuyudong/p/5898403.html,轉載請標註來
仿 QQ 介面實作:底部導覽與訊息列表的還原練習
前幾天一直跟朋友聚會,專案擱著沒動,今天終於有時間開電腦繼續做。以下是目前做出來的效果。 這個練習是照著 QQ 的介面做的。因為牽涉到的檔案不少、程式碼加起來很長,
Android Activity 與 Service 資料互動:bindService 實作
Activity 與 Service 互動 Android 中啟動 Service 主要有 startService 與 bindService。startService