Android用PopupWindow实现自定义Dailog

所属分类: 软件编程 / Android 阅读数: 18
收藏 0 赞 0 分享

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实现悬浮窗体效果

这篇文章主要为大家详细介绍了Android实现悬浮窗体效果,显示悬浮窗口,窗口可以拖动,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Andriod studio 打包aar 的方法

这篇文章主要介绍了Andriod studio 打包aar的方法,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

Android加载loading对话框的功能及实例代码(不退出沉浸式效果)

这篇文章主要介绍了Android加载loading对话框的功能及实例代码,不退出沉浸式效果,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中LayoutInflater.inflater()的正确打开方式

这篇文章主要给大家介绍了关于Android中LayoutInflater.inflater()的正确打开方式,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Delphi在Android下使用Java库的方法

这篇文章主要介绍了Delphi在Android下使用Java库的方法,本文以Android的USB串口通讯库为例,给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多