Android用PopupWindow实现自定义Dailog

#技术教程 发布时间: 2026-01-18

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的实现就介绍到这里了。

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




上一篇 : 微信小程序 scroll-view实现上拉加载与下拉刷新的实例

下一篇 : 详谈Android动画效果translate、scale、alpha、rotate

推荐阅读

电话:400 76543 55
邮箱:915688610@qq.com
品牌营销
客服微信
搜索营销
公众号
©  丽景创新 版权所有 赣ICP备2024032158号 
宜昌市隼壹珍商贸有限公司 宜昌市隼壹珍商贸有限公司 宜昌市隼壹珍商贸有限公司 宜昌市隼壹珍商贸有限公司 宜昌市隼壹珍商贸有限公司 宜昌市隼壹珍商贸有限公司 宜昌市隼壹珍商贸有限公司 宜昌市隼壹珍商贸有限公司 宜昌市隼壹珍商贸有限公司 宜昌市隼壹珍商贸有限公司 内江振祥营销策划有限公司 内江振祥营销策划有限公司 内江振祥营销策划有限公司 内江振祥营销策划有限公司 内江振祥营销策划有限公司 内江振祥营销策划有限公司 内江振祥营销策划有限公司 内江振祥营销策划有限公司 内江振祥营销策划有限公司 内江振祥营销策划有限公司 内江振祥营销策划有限公司 内江振祥营销策划有限公司 内江振祥营销策划有限公司 内江振祥营销策划有限公司 内江振祥营销策划有限公司 内江振祥营销策划有限公司 恩施州毯滚百货有限公司 恩施州毯滚百货有限公司 襄阳市蜂欢商贸有限公司 襄阳市蜂欢商贸有限公司 恩施州换冯百货有限公司 恩施州换冯百货有限公司 恩施州健提百货有限公司 恩施州健提百货有限公司 西安益零商贸有限公司 西安益零商贸有限公司 南奥教育 南奥教育 南奥教育 南奥教育 南昌市南奥教育咨询有限公司 南昌市南奥教育咨询有限公司 南昌市南奥教育咨询有限公司 南昌市南奥教育咨询有限公司 南昌市南奥教育咨询有限公司 南昌市南奥教育咨询有限公司 南昌市南奥教育咨询有限公司 南昌市南奥教育咨询有限公司 南奥教育网 南奥教育网 南奥教育网 南奥教育网 南奥学习网 南奥学习网 南奥学习网 南奥学习网 南奥教育 南奥教育 南奥留学记 南奥留学记 南奥教育 南奥教育 南昌市南奥教育咨询有限公司 南昌市南奥教育咨询有限公司 南昌市南奥教育咨询有限公司 南昌市南奥教育咨询有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 南昌壹佳企网络通信有限公司 广照天下广告 广照天下广告 广照天下广告策划 广照天下广告策划 广照天下 广照天下 广照天下 广照天下 广照天下 广照天下 广照天下广告策划 广照天下广告策划 广照天下广告策划 广照天下广告策划 南昌市广照天下广告策划有限公司 南昌市广照天下广告策划有限公司 南昌市广照天下广告策划有限公司 南昌市广照天下广告策划有限公司 宿州市腾雀网络科技有限公司 宿州市腾雀网络科技有限公司 宿州市腾雀网络科技有限公司 宿州市腾雀网络科技有限公司 宿州市腾雀网络科技有限公司 宿州市腾雀网络科技有限公司 宿州市腾雀网络科技有限公司 宿州市腾雀网络科技有限公司 宿州市腾雀网络科技有限公司 宿州市腾雀网络科技有限公司 宿州市腾雀网络科技有限公司 宿州市腾雀网络科技有限公司 宿州市腾雀网络科技有限公司 宿州市腾雀网络科技有限公司 宿州市腾雀网络科技有限公司 宿州市腾雀网络科技有限公司 宿州市腾雀网络科技有限公司 宿州市腾雀网络科技有限公司 九江市云仁商务咨询有限公司 九江市云仁商务咨询有限公司 九江市云仁商务咨询有限公司 九江市云仁商务咨询有限公司 九江市云仁商务咨询有限公司 九江市云仁商务咨询有限公司 九江市云仁商务咨询有限公司 九江市云仁商务咨询有限公司 九江市云仁商务咨询有限公司 九江市云仁商务咨询有限公司
品牌营销
专业SEO优化
添加左侧专家微信
获取产品详细报价方案