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的長寬 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手機(jī)衛(wèi)士之抖動(dòng)輸入框和手機(jī)震動(dòng)
這篇文章主要介紹了淺析Android手機(jī)衛(wèi)士之輸入框抖動(dòng)和手機(jī)震動(dòng)的相關(guān)資料,需要的朋友可以參考下2016-04-04Android中毛玻璃效果的兩種實(shí)現(xiàn)代碼
這篇文章主要介紹了Android中毛玻璃效果的兩種實(shí)現(xiàn)代碼,第一種是使用JAVA算法FastBlur實(shí)現(xiàn),第二種是使用Android自帶類RenderScript 實(shí)現(xiàn),本文通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友參考下吧2024-08-08Android開發(fā)仿掃一掃實(shí)現(xiàn)拍攝框內(nèi)的照片功能
無論是微信還是支付寶掃一掃功能很常用,那么它基于代碼是如何實(shí)現(xiàn)的呢?今天小編給大家分享android開發(fā)之仿掃一掃實(shí)現(xiàn)拍攝框內(nèi)的照片功能,感興趣的朋友一起學(xué)習(xí)吧2016-09-09Android項(xiàng)目實(shí)戰(zhàn)之ListView懸浮頭部展現(xiàn)效果實(shí)現(xiàn)
這篇文章主要給大家介紹了Android項(xiàng)目實(shí)戰(zhàn)之ListView懸浮頭部展現(xiàn)效果實(shí)現(xiàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01淺談Android onTouchEvent 與 onInterceptTouchEvent的區(qū)別詳解
本篇文章小編為大家介紹,Android onTouchEvent 與 onInterceptTouchEvent的區(qū)別詳解。需要的朋友參考下2013-04-04Android側(cè)滑菜單控件DrawerLayout使用詳解
這篇文章主要為大家詳細(xì)介紹了Android側(cè)滑菜單控件DrawerLayout的使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android自定義View實(shí)現(xiàn)彈幕效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)彈幕效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11Android ApplicationInfo 應(yīng)用程序信息的詳解
這篇文章主要介紹了Android ApplicationInfo 應(yīng)用程序信息的詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10Android ContentProvider獲取手機(jī)聯(lián)系人實(shí)例
這篇文章主要介紹了Android ContentProvider獲取手機(jī)聯(lián)系人實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02