Android用PopupWindow实现自定义Dailog

发布时间 - 2026-01-10 22:41:52    点击率:

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

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


(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, 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 和 Activity弹出窗口实现方式  # android popwindow实现左侧弹出菜单层及PopupWindow主要方法介绍  # Android Animation实战之屏幕底部弹出PopupWindow  # Android入门之PopupWindow用法实例解析  # Android之用PopupWindow实现弹出菜单的方法详解  # Android编程实现popupwindow弹出后屏幕背景变成半透明效果  # Android PopupWindow 点击外面取消实现代码  # android使用PopupWindow实现页面点击顶部弹出下拉菜单  # Android中PopupWindow响应返回键并关闭的2种方法  # android教程之使用popupwindow创建菜单示例  # Android中自定义PopupWindow实现弹出框并带有动画效果  # Android编程中PopupWindow的用法分析【位置、动画、焦点】  # Android编程之PopupWindow隐藏及显示方法示例(showAtLocation  # showAsDropDown)  # 对话框  # 方法如下  # 是个  # 你可以  # 弹出  # 还有一个  # 做一个  # 它可以  # 所示  # 到此为止  # 最简单  # 来实现  # 主要包括  # 最常见  # 长宽  # 大家多多  # 就可以  # 很有用  # 视频播放  # 有一个 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 免费制作统计图的网站有哪些,如何看待现如今年轻人买房难的情况?  如何快速搭建支持数据库操作的智能建站平台?  Laravel如何配置任务调度?(Cron Job示例)  Laravel事件监听器怎么写_Laravel Event和Listener使用教程  微信小程序 canvas开发实例及注意事项  如何快速搭建FTP站点实现文件共享?  香港服务器部署网站为何提示未备案?  如何快速生成ASP一键建站模板并优化安全性?  C++时间戳转换成日期时间的步骤和示例代码  如何挑选高效建站主机与优质域名?  Laravel Artisan命令怎么自定义_创建自己的Laravel命令行工具完全指南  Laravel如何配置中间件Middleware_Laravel自定义中间件拦截请求与权限校验【步骤】  高性能网站服务器部署指南:稳定运行与安全配置优化方案  专业商城网站制作公司有哪些,pi商城官网是哪个?  Laravel如何使用Service Container和依赖注入?(代码示例)  如何在VPS电脑上快速搭建网站?  深圳网站制作设计招聘,关于服装设计的流行趋势,哪里的资料比较全面?  利用python获取某年中每个月的第一天和最后一天  javascript中闭包概念与用法深入理解  EditPlus中的正则表达式实战(5)  Laravel如何创建和注册中间件_Laravel中间件编写与应用流程  Laravel如何实现API版本控制_Laravel API版本化路由设计策略  简历没回改:利用AI润色让你的文字更专业  悟空浏览器如何设置小说背景色_悟空浏览器背景色设置【方法】  网站设计制作书签怎么做,怎样将网页添加到书签/主页书签/桌面?  今日头条微视频如何找选题 今日头条微视频找选题技巧【指南】  Win11摄像头无法使用怎么办_Win11相机隐私权限开启教程【详解】  Laravel项目怎么部署到Linux_Laravel Nginx配置详解  Laravel如何创建自定义Artisan命令?(代码示例)  最好的网站制作公司,网购哪个网站口碑最好,推荐几个?谢谢?  如何快速搭建个人网站并优化SEO?  Win10如何卸载预装Edge扩展_Win10卸载Edge扩展教程【方法】  Laravel如何实现全文搜索功能?(Scout和Algolia示例)  Laravel怎么实现搜索高亮功能_Laravel结合Scout与Algolia全文检索【实战】  JS中对数组元素进行增删改移的方法总结  大学网站设计制作软件有哪些,如何将网站制作成自己app?  详解Android中Activity的四大启动模式实验简述  想要更高端的建设网站,这些原则一定要坚持!  北京企业网站设计制作公司,北京铁路集团官方网站?  桂林网站制作公司有哪些,桂林马拉松怎么报名?  Win11怎么恢复误删照片_Win11数据恢复工具使用【推荐】  PHP 实现电台节目表的智能时间匹配与今日/明日轮播逻辑  在centOS 7安装mysql 5.7的详细教程  Laravel辅助函数有哪些_Laravel Helpers常用助手函数大全  Laravel怎么连接多个数据库_Laravel多数据库连接配置  如何用5美元大硬盘VPS安全高效搭建个人网站?  实例解析Array和String方法  如何基于云服务器快速搭建个人网站?  网站制作软件免费下载安装,有哪些免费下载的软件网站?  Laravel如何使用Gate和Policy进行权限控制_Laravel权限判定与策略规则配置