Android用PopupWindow實(shí)現(xiàn)自定義Dailog
Android的PopupWindow是個(gè)很有用的widget,利用它可以實(shí)現(xiàn)懸浮窗體的效果,比如實(shí)現(xiàn)一個(gè)懸浮的菜單,最常見的應(yīng)用就是在視頻播放界面里,做一個(gè)工具欄,用來控制播放進(jìn)度。本文利用PopupWindow來實(shí)現(xiàn)一個(gè)通用的Dailog,類似Android系統(tǒng)的AlertDailog,從中學(xué)習(xí)和掌握有關(guān)PopupWindow和Dailog的使用和實(shí)現(xiàn)細(xì)節(jié)。
界面效果如圖所示,點(diǎn)擊 Click 按鈕后,彈出對(duì)話框提示。

(1). CustomDailog的布局
首先定義 CustDailog的布局文件,由系統(tǒng)的AlertDailog可以知道,一個(gè)對(duì)話框包含了三個(gè)要素,一個(gè)是Title,即標(biāo)題,一個(gè)是Message,即主體內(nèi)容,還有一個(gè)是Button,即確定和取消的按鈕,用來與用戶交互。因此,布局設(shè)計(jì)如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://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:textStyle="bold"
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>
其中,shap_bg.xml 是Dailog的背景的定義文件,你可以修改此文件,來改變Dailog的背景:
<?xml version="1.0" encoding="UTF-8"?> <shape android:shape="rectangle" xmlns:android="http://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). CustomDailog的定義
CustomDailog的接口,可以類比AlertDailg的接口定義,主要包括如下一些方法:
1. setTitle 設(shè)置標(biāo)題
2. setMessage 設(shè)置主體內(nèi)容
3. setPositiveButton 設(shè)置 “確定” 按鈕
4. setNegativeButton 設(shè)置 “取消” 按鈕
5. show 顯示
6. dimiss 消失
其定義如下:
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必須有一個(gè)ParentView,所以必須添加這個(gè)參數(shù)
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);
}
//設(shè)置Dailog的標(biāo)題
public void setTitle(String title) {
TextView mTitle = (TextView)mRootLayout.findViewById(R.id.CustomDlgTitle);
mTitle.setText(title);
}
//設(shè)置Dailog的主體內(nèi)容
public void setMessage(String message) {
TextView mMessage = (TextView)mRootLayout.findViewById(R.id.CustomDlgContentText);
mMessage.setText(message);
}
//設(shè)置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);
}
//設(shè)置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);
}
//設(shè)置Dailog的長(zhǎng)寬
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 的顯示必須給一個(gè)ParentView,在Activity中使用的話,最簡(jiǎn)單的方法就是將整個(gè)activity的“根View”傳遞給這個(gè)PopupWindow,這樣就可以在整個(gè)屏幕的正中央來顯示Dailog,獲取Acitivity的根View的方法如下:
findViewById(android.R.id.content)).getChildAt(0);
因此,上面定義的 CunstomDailog的使用方法如下所示:
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();
到此為止,整個(gè)Dailog的實(shí)現(xiàn)就介紹到這里了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android PopupWindow 和 Activity彈出窗口實(shí)現(xiàn)方式
- android popwindow實(shí)現(xiàn)左側(cè)彈出菜單層及PopupWindow主要方法介紹
- Android Animation實(shí)戰(zhàn)之屏幕底部彈出PopupWindow
- Android入門之PopupWindow用法實(shí)例解析
- Android之用PopupWindow實(shí)現(xiàn)彈出菜單的方法詳解
- Android編程實(shí)現(xiàn)popupwindow彈出后屏幕背景變成半透明效果
- Android PopupWindow 點(diǎn)擊外面取消實(shí)現(xiàn)代碼
- android使用PopupWindow實(shí)現(xiàn)頁面點(diǎn)擊頂部彈出下拉菜單
- Android中PopupWindow響應(yīng)返回鍵并關(guān)閉的2種方法
- android教程之使用popupwindow創(chuàng)建菜單示例
- Android中自定義PopupWindow實(shí)現(xiàn)彈出框并帶有動(dòng)畫效果
- Android編程中PopupWindow的用法分析【位置、動(dòng)畫、焦點(diǎn)】
- Android編程之PopupWindow隱藏及顯示方法示例(showAtLocation,showAsDropDown)
相關(guān)文章
Android ProgressBar實(shí)現(xiàn)進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android ProgressBar實(shí)現(xiàn)進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android Studio 運(yùn)行按鈕灰色的完美解決方法
今天新建項(xiàng)目的時(shí)候突然發(fā)現(xiàn)編譯后運(yùn)行按鈕為灰色,今天小編給大家?guī)砹薃ndroid Studio 運(yùn)行按鈕灰色的完美解決方法,非常不錯(cuò),對(duì)大家的需要或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-10-10
Android Fragment動(dòng)態(tài)創(chuàng)建詳解及示例代碼
這篇文章主要介紹了Android Fragment動(dòng)態(tài)創(chuàng)建詳解的相關(guān)資料,并附實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-11-11
Android實(shí)例HandlerThread源碼分析
本篇文章主要給大家通過實(shí)例代碼分析了Android中HandlerThread的用法以及步驟,需要的朋友參考學(xué)習(xí)下吧。2017-12-12
Android和JavaScript相互調(diào)用的方法
這篇文章主要介紹了Android和JavaScript相互調(diào)用的方法,實(shí)例分析了Android的WebView執(zhí)行JavaScript及JavaScript訪問Android的技巧,需要的朋友可以參考下2015-12-12
Android Notification實(shí)現(xiàn)動(dòng)態(tài)顯示通話時(shí)間
這篇文章主要為大家詳細(xì)介紹了Android Notification實(shí)現(xiàn)動(dòng)態(tài)顯示通話時(shí)間,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Android將項(xiàng)目導(dǎo)出為L(zhǎng)ibrary并在項(xiàng)目中使用教程
這篇文章主要介紹了Android將項(xiàng)目導(dǎo)出為L(zhǎng)ibrary并在項(xiàng)目中使用教程,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07
Flutter源碼分析之自定義控件(RenderBox)指南
寫了兩天的flutter,發(fā)現(xiàn)控件樣式很多,flutter資源很少,本文在于實(shí)用性,可以減少頁面代碼,下面這篇文章主要介紹了Flutter源碼分析之自定義控件(RenderBox)的相關(guān)資料,需要的朋友可以參考下2021-08-08

