Android用PopupWindow实现自定义Dailog

Android用PopupWindow实现自定义Dailog,第1张

概述Android的PopupWindow是个很有用的widget,利用它可以实现悬浮窗体的效果,比如实现一个悬浮的菜单,最常见的应用就是在视频播放界面里,做一个工具栏,用来控制播放进度。本文利用PopupWindow来实现一个通用的Dailo

AndroID的PopupWindow是个很有用的Widget,利用它可以实现悬浮窗体的效果,比如实现一个悬浮的菜单,最常见的应用就是在视频播放界面里,做一个工具栏,用来控制播放进度。本文利用PopupWindow来实现一个通用的Dailog,类似AndroID系统的AlertDailog,从中学习和掌握有关PopupWindow和Dailog的使用和实现细节。

界面效果如图所示,点击 Click 按钮后,d出对话框提示。


(1).  CustomDailog的布局

首先定义 CustDailog的布局文件,由系统的AlertDailog可以知道,一个对话框包含了三个要素,一个是Title,即标题,一个是Message,即主体内容,还有一个是button,即确定和取消的按钮,用来与用户交互。因此,布局设计如下:

<?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 设置标题
2.  setMessage 设置主体内容
3.  setPositivebutton 设置 “确定” 按钮
4.  setNegativebutton 设置 “取消” 按钮
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必须有一个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);  }                                                                          //取消Dailog的显示  public voID dismiss() {                                                                              if(mPopupWindow == null) {      return;    }                                                                              mPopupWindow.dismiss();  }}

(3). 在Activity中的使用方法

由于 PopupWindow 的显示必须给一个ParentVIEw,在Activity中使用的话,最简单的方法就是将整个activity的“根VIEw”传递给这个PopupWindow,这样就可以在整个屏幕的正中央来显示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();

到此为止,整个Dailog的实现就介绍到这里了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android用PopupWindow实现自定义Dailog全部内容,希望文章能够帮你解决Android用PopupWindow实现自定义Dailog所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1147206.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存