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)
# 对话框
# 方法如下
# 是个
# 你可以
# 弹出
# 还有一个
# 做一个
# 它可以
# 所示
# 到此为止
# 最简单
# 来实现
# 主要包括
# 最常见
# 长宽
# 大家多多
# 就可以
# 很有用
# 视频播放
# 有一个
相关文章:
javascript基本数据类型及类型检测常用方法小结
网站代码制作软件有哪些,如何生成自己网站的代码?
如何选择最佳自助建站系统?快速指南解析优劣
深圳防火门网站制作公司,深圳中天明防火门怎么编码?
平台云上自助建站如何快速打造专业网站?
建站主机选择指南:服务器配置与SEO优化实战技巧
东莞市网站制作公司有哪些,东莞找工作用什么网站好?
Android使用GridView实现日历的简单功能
手机怎么制作网站教程步骤,手机怎么做自己的网页链接?
网站设计制作书签怎么做,怎样将网页添加到书签/主页书签/桌面?
如何用好域名打造高点击率的自主建站?
详解一款开源免费的.NET文档操作组件DocX(.NET组件介绍之一)
无锡制作网站公司有哪些,无锡优八网络科技有限公司介绍?
东莞专业网站制作公司有哪些,东莞招聘网站哪个好?
如何在阿里云香港服务器快速搭建网站?
北京营销型网站制作公司,可以用python做一个营销推广网站吗?
宝盒自助建站智能生成技巧:SEO优化与关键词设置指南
广德云建站网站建设方案与建站流程优化指南
存储型VPS适合搭建中小型网站吗?
云南网站制作公司有哪些,云南最好的招聘网站是哪个?
常州企业网站制作公司,全国继续教育网怎么登录?
网站好制作吗知乎,网站开发好学吗?有什么技巧?
韩国代理服务器如何选?解析IP设置技巧与跨境访问优化指南
如何用狗爹虚拟主机快速搭建网站?
C#如何在一个XML文件中查找并替换文本内容
如何用已有域名快速搭建网站?
建站之星Pro快速搭建教程:模板选择与功能配置指南
宿州网站制作公司兴策,安徽省低保查询网站?
网站制作大概要多少钱一个,做一个平台网站大概多少钱?
整蛊网站制作软件,手机不停的收到各种网站的验证码短信,是手机病毒还是人为恶搞?有这种手机病毒吗?
网站制作的步骤包括,正确网址格式怎么写?
c++ stringstream用法详解_c++字符串与数字转换利器
如何在IIS7中新建站点?详细步骤解析
如何通过FTP空间快速搭建安全高效网站?
品牌网站制作公司有哪些,买正品品牌一般去哪个网站买?
大学网站设计制作软件有哪些,如何将网站制作成自己app?
重庆市网站制作公司,重庆招聘网站哪个好?
微信小程序 input输入框控件详解及实例(多种示例)
如何选择适配移动端的WAP自助建站平台?
如何制作一个表白网站视频,关于勇敢表白的小标题?
如何通过商城免费建站系统源码自定义网站主题?
我的世界制作壁纸网站下载,手机怎么换我的世界壁纸?
简历在线制作网站免费版,如何创建个人简历?
网站微信制作软件,如何制作微信链接?
如何通过VPS搭建网站快速盈利?
电商网站制作多少钱一个,电子商务公司的网站制作费用计入什么科目?
如何在万网自助建站中设置域名及备案?
专业网站设计制作公司,如何制作一个企业网站,建设网站的基本步骤有哪些?
深圳网站制作案例,网页的相关名词有哪些?
建站上市公司网站建设方案与SEO优化服务定制指南
*请认真填写需求信息,我们会在24小时内与您取得联系。