Android用PopupWindow实现自定义Dailog
作者:风继续吹_z 发布时间:2023-11-03 13:34:57
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提供了View来进行绘图处理,在大部分情况下,View都能满足绘图需求。大家都知道View是通过刷新来重绘视图,An
- Android 版本更替,新的版本带来新的特性,新的方法。新的方法带来许多便利,但无法在低版本系统上运行,如果兼容性处理不恰当,APP在低版
- Android 网络请求框架Volley实例详解首先上效果图Logcat日志信息on ReponseVolley特别适合数据量不大但是通信频
- import java.util.ArrayList;import java.util.HashMap;import java.util.I
- 基于SSM框架的仓库管理系统功能:系统操作权限管理。系统提供基本的登入登出功能,同时系统包含两个角色:系统超级管理员和普通管理员,超级管理员
- 目录前言整合swagger api自定义配置信息简单使用Swagger常用注解Api标记ApiOperation标记ApiParam标记Ap
- 1、什么是GradleGradle是一种结合了Ant和Maven两者优势的下一代构建工具,既有Ant构建灵活性的优点,也保留Maven约定优
- Spring Boot除了可以打可执行jar包外,也支持传统的war包。本文介绍如何使用Spring Boot构建传统war包。Spring
- (一)打包与运行SpringBoot项目快速启动(Linux版)基于Linux (CenteroS7)安装JDK,且版本不低于打包时使用的J
- 1.1 接口组成更新概述接口的组成常量:public static final抽象方法:public abstract默认方法(Java 8
- 本文实例为大家分享了Android自定义ViewGroup多行多列的具体代码,供大家参考,具体内容如下先看下效果图每行两个子孩子每行一个子孩
- 本文以案例形式分析了Android中TelephonyManager类的用法。分享给大家供大家参考。具体如下:目录结构:main.xml布局
- 在业务开发过程中我们会遇到形形色色的注解,但是框架自有的注解并不是总能满足复杂的业务需求,我们可以自定义注解来满足我们的需求。根据注解使用的
- 本文实例讲述了Java中的匿名对象定义与用法。分享给大家供大家参考,具体如下:一 点睛匿名对象是指没有名字的对象。实际上,对于对象实例化操作
- 目录SpringBoot整合OpenApiOpenAPI依赖编写配置类改造优化OpenAPI常用注解介绍实体类controller类演示网上
- 本文实例讲述了Android编程之DatePicker和TimePicke简单时间监听用法。分享给大家供大家参考,具体如下:DatePick
- 通过JDK的Proxy代理实现对业务类做简单的AOP实现接口:UserService 包含的方法为切入点,会被代理拦截类:UserServi
- 在Struts2中Action部分,也就是Controller层采用了低侵入的方式。为什么这么说?这是因为在Struts2中action类并
- 摘要:1.日志输出到文件并根据LEVEL级别将日志分类保存到不同文件2.通过异步输出日志减少磁盘IO提高性能3.异步输出日志的原理1、配置文
- 依赖的jar包:spring相关jar包版本:4.3.1.RELEASE<dependency> &l