編輯:進階開發
前文我們已經談過如何在 Android 中打造帶圖片與勾選框的 ListView,那麼該怎麼把 ListView 放進 PopupWindow 裡呢?關於 ListView 本身就不再贅述,這次的重點在於實作 PopupWindow,並美化它的外觀。
先看看成品截圖:

1、建立一個 PopupWindow,並設定相應的樣式。
Java 程式碼
- private void popWindow(View parent) {
- if (window == null) {
- LayoutInflater lay = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View v = lay.inflate(R.layout.popupwindow, null);
- v.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_corners_view));
- //初始化按鈕
- submit = (Button) v.findViewById(R.id.submit);
- submit.setOnClickListener(submitListener);
- cancel = (Button) v.findViewById(R.id.cancel);
- cancel.setOnClickListener(cancelListener);
- //初始化 ListView,載入資料。
- list=(ListView)v.findViewById(R.id.lv);
- MyAdapter adapter=new MyAdapter(Main.this);
- list.setAdapter(adapter);
- list.setItemsCanFocus(false);
- list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
- list.setOnItemClickListener(listClickListener);
- window = new PopupWindow(v, 500,260);
- }
- //設定整個 PopupWindow 的樣式。
- window.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_corners_pop));
- //讓視窗內的元件呈現相應效果,例如點擊 button 時背景色改變。
- //若為 false,點擊元件時表面沒有反應,但事件仍可監聽到。
- //對 ListView 而言則無此作用。
- window.setFocusable(true);
- window.update();
- window.showAtLocation(parent, Gravity.CENTER_VERTICAL, 0, 0);
- }
- OnItemClickListener listClickListener = new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position,
- long id) {
- ViewHolder vHolder = (ViewHolder) view.getTag();
- //每次點擊 item 時切換對應 checkbox 狀態,並同步更新 map 的值。
- vHolder.cBox.toggle();
- MyAdapter.isSelected.put(position, vHolder.cBox.isChecked());
- }
- };
為按鈕加上監聽事件:
Java 程式碼
- OnClickListener submitListener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- //這裡可以寫提交資料的程式碼。
- closeWindow();
- }
- };
- OnClickListener cancelListener=new OnClickListener(){
- @Override
- public void onClick(View v){
- closeWindow();
- }
- };
- private void closeWindow(){
- //把每個 checkbox 的標記改為 false,讓下次彈出視窗時回到初始狀態。
- for (int j = 0; j < MyAdapter.isSelected.size(); j++) {
- MyAdapter.isSelected.put(j, false);
- ViewHolder vHolder = (ViewHolder) list.getChildAt(j).getTag();
- vHolder.cBox.setChecked(false);
- }
- //提交資料後關閉 PopupWindow。
- if (window != null) {
- window.dismiss();
- }
- }
在 layout 目錄中新建 popupwindow.XML 檔案,主要負責視窗的排版:
Java 程式碼
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView
- android:id="@+id/tip"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:textSize="18dip"
- android:background="@drawable/rounded_corners_list"
- android:text="這是一個 PopupWindow 的例子"/>
- <!-- 當 layout_width 為 fill_parent 時,居中要用 android:gravity="center" -->
- <ListView
- android:id="@+id/lv"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/rounded_corners_list"/>
- <LinearLayout
- android:orientation="horizontal"
- android:layout_gravity="center"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/rounded_corners_list">
- <Button
- android:id="@+id/submit"
- android:layout_width="100dip"
- android:layout_height="50dip"
- android:text="提交"/>
- <Button
- android:id="@+id/cancel"
- android:layout_width="100dip"
- android:layout_height="50dip"
- android:text="取消"/>
- </LinearLayout>
- </LinearLayout>
再建立 rounded_corners_pop.XML,用於自訂視窗外觀:
Java 程式碼
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="https://schemas.android.com/apk/res/android">
- <solid android:color="#ffffffff" />
- <stroke android:width="3dp" color="#ffff8080" />
- <corners android:radius="10dp" />
- <padding
- android:left="3dp"
- android:top="3dp"
- android:right="3dp"
- android:bottom="3dp" />
- </shape>
這樣就能做出圓角效果,而外圍的白邊則是透過在白色樣式上疊加一層黑色來呈現的。其他樣式檔大家可以參考 rounded_corners_pop.XML 自行發揮。
2、在 main.XML 中加入兩個按鈕,一個用來顯示視窗,一個用來隱藏視窗。
Java 程式碼
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@+id/lmain"
- >
- <Button
- android:id="@+id/myButton1"
- android:layout_width="100dip"
- android:layout_height="50dip"
- android:text="顯示"/>
- <Button
- android:id="@+id/myButton2"
- android:layout_width="100dip"
- android:layout_height="50dip"
- android:text="隱藏"/>
- </LinearLayout>
在 Activity 中初始化這兩個按鈕並加上監聽事件:
Java 程式碼
- OnClickListener bPop = new OnClickListener() {
- @Override
- public void onClick(View v) {
- popWindow(v);
- }
- };
- OnClickListener boff = new OnClickListener() {
- @Override
- public void onClick(View v) {
- if(window!=null){
- window.dismiss();
- }
- }
- };
SurfaceView 繪圖攻略:覆蓋刷新與髒矩形刷新實作
在 Android 上做遊戲,SurfaceView 幾乎是標配。它最大的好處是擁有獨立的繪圖表面,可以在自己的執行緒裡不停重畫,不必受 View 單執行緒模型拖累。以下
NDK 入門專案實戰(二)
hello-jni 為 apps 下 hello-jni 資料夾的名稱。這個指令會先找到 apps\hello-jni 下的 Application.mk,再找到 sou
Android 開發者將獲 Sprint 網路資源支持
Android 近來火熱,這也讓 Android 開發者面臨更大的開發壓力。為了更好地幫助廣大開發者,Sprint 將提供網路資源支持。 一般而言,手機作業系統廠商會向開
DDMS 工作原理拆解:埠號分配、Devices 面板與檔案總管
DDMS 為 IDE 搭建起通往測試終端的連結,雙方各自監聽獨立埠口來交換除錯資訊。它最大的特性,是能即時感知測試終端的連線狀態,並自動列出新加入的裝置。本文拆解它的埠號