編輯:關於 Android 程式設計
Android的PopupWindow是一個非常實用的widget,利用它可以實現懸浮窗體效果,比如做一個懸浮菜單。最常見的應用場景是在視頻播放界面裡做一個工具欄,用來控制播放進度。本文就借助PopupWindow來實現一個通用的Dialog,效果類似系統的AlertDialog,藉此學習PopupWindow和Dialog的使用與實現細節。
界面效果如下圖所示,點擊Click按鈕後彈出對話框提示。

(1)CustomDialog的布局
先定義CustomDialog的布局文件。參考系統的AlertDialog可知,一個對話框包含三個要素:Title(標題)、Message(主體內容)以及Button(確定與取消按鈕,用於與用戶交互)。因此布局設計如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:background="@drawable/shape_bg" android:layout_margin="10dp"> <TextView android:id="@+id/CustomDlgTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text android:textSize="20sp" android:layout_margin="10dp" android:gravity="center"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/darker_gray"/> <LinearLayout android:id="@+id/CustomDlgContentView" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_margin="5dp" /> <TextView android:id="@+id/CustomDlgContentText" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="15sp" android:layout_margin="5dp" android:paddingLeft="5sp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_margin="5dp" > <Button android:id="@+id/CustomDlgButtonOK" android:layout_width="0dp" android:layout_weight="0.5" android:layout_height="wrap_content" android:visibility="gone"/> <Button android:id="@+id/CustomDlgButtonCancel" android:layout_width="0dp" android:layout_weight="0.5" android:layout_height="wrap_content" android:visibility="gone"/> </LinearLayout> </LinearLayout>
其中shape_bg.xml是Dialog背景的定义文件,修改它即可改變Dialog的背景樣式:
<?xml version="1.0" encoding="UTF-8"?> <shape android:shape="rectangle" xmlns:android="https://schemas.android.com/apk/res/android"> <solid android:color="#e6ecee" /> <stroke android:width="1.0dip" android:color="@android:color/darker_gray" /> <corners android:radius="8.0dip" /> </shape>
(2)CustomDialog的定義
CustomDialog的接口可以類比AlertDialog來設計,主要包括以下方法:
1. setTitle 設置標題
2. setMessage 設置主體內容
3. setPositiveButton 設置「確定」按鈕
4. setNegativeButton 設置「取消」按鈕
5. show 顯示
6. dismiss 消失
具體定義如下:
package com.ticktick.popdailog; import android.content.Context; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.TextView; public class CustomDailog { private View mParent; private PopupWindow mPopupWindow; private LinearLayout mRootLayout; private LayoutParams mLayoutParams; //PopupWindow必須有一個ParentView,所以必須添加這個參數 public CustomDailog(Context context, View parent) { mParent = parent; LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //加載布局文件 mRootLayout = (LinearLayout)mInflater.inflate(R.layout.custom_dailog, null); mLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); } //設置Dailog的標題 public void setTitle(String title) { TextView mTitle = (TextView)mRootLayout.findViewById(R.id.CustomDlgTitle); mTitle.setText(title); } //設置Dailog的主體內容 public void setMessage(String message) { TextView mMessage = (TextView)mRootLayout.findViewById(R.id.CustomDlgContentText); mMessage.setText(message); } //設置Dailog的“確定”按鈕 public void setPositiveButton(String text,OnClickListener listener ) { final Button buttonOK = (Button)mRootLayout.findViewById(R.id.CustomDlgButtonOK); buttonOK.setText(text); buttonOK.setOnClickListener(listener); buttonOK.setVisibility(View.VISIBLE); } //設置Dailog的“取消”按鈕 public void setNegativeButton(String text,OnClickListener listener ) { final Button buttonCancel = (Button)mRootLayout.findViewById(R.id.CustomDlgButtonCancel); buttonCancel.setText(text); buttonCancel.setOnClickListener(listener); buttonCancel.setVisibility(View.VISIBLE); } //替換Dailog的“主體”布局 public void setContentLayout(View layout) { TextView mMessage = (TextView)mRootLayout.findViewById(R.id.CustomDlgContentText); mMessage.setVisibility(View.GONE); LinearLayout contentLayout = (LinearLayout)mRootLayout.findViewById(R.id.CustomDlgContentView); contentLayout.addView(layout); } //設置Dailog的長寬 public void setLayoutParams(int width, int height) { mLayoutParams.width = width; mLayoutParams.height = height; } //顯示Dailog public void show() { if(mPopupWindow == null) { mPopupWindow = new PopupWindow(mRootLayout, mLayoutParams.width,mLayoutParams.height); mPopupWindow.setFocusable(true); } mPopupWindow.showAtLocation(mParent, Gravity.CENTER, Gravity.CENTER, Gravity.CENTER); } //取消Dailog的顯示 public void dismiss() { if(mPopupWindow == null) { return; } mPopupWindow.dismiss(); } }
(3)在Activity中的使用方法
由於PopupWindow顯示時必須指定一個ParentView,在Activity中使用時最簡單的做法就是把整個Activity的根View傳給它,這樣就能讓Dialog顯示在屏幕正中央。獲取Activity根View的方法如下:
findViewById(android.R.id.content)).getChildAt(0);
因此,上面定義的CustomDialog的使用方式如下:
final CustomDailog dailog = new CustomDailog(this,getRootLayout()); dailog.setTitle("Warning"); dailog.setMessage("This is ticktick's blog!"); dailog.setPositiveButton("OK", new OnClickListener() { @Override public void onClick(View v) { dailog.dismiss(); } }); dailog.setNegativeButton("Cancel", new OnClickListener() { @Override public void onClick(View v) { dailog.dismiss(); } }); dailog.show();
至此,整個Dialog的實現就介紹完了。
以上就是本文的全部內容,希望對大家的學習有所幫助。
微信小視頻怎麼壓到 1M 以內?手機與電腦兩套方案
微信朋友圈對影片大小與時長都有限制,把檔案壓到 1MB 以內就有了操作空間。實務上有兩條路:用手機直接壓,或用電腦壓得更精細。 方案一:手機端壓縮 手機壓縮不如電腦精細,
TextView 使用 SpannableString 實作可點擊文本
TextView 不只是用來顯示純文字,也能承載樣式與互動。透過 SpannableString,可以讓部分文字變色、加上底線,甚至變成可點擊的連結。 常見 Span
自定義 View 完全指南:測量、佈局與繪製
在 Android 開發中,自定義 View 幾乎是每個開發者都會遇到的進階主題。當系統自帶控件無法滿足業務需求時,我們就需要自己動手實現一個 View。要真正掌握自定義
Android ApiDemo 範例工程的建立與導覽
幾乎每套 SDK 都會附上範例程式來示範 API 用法,Android SDK 也不例外。它內建的 ApiDemo 把平台主要 API 拆成七大類,每個大類底下再細分小類