自定义Dialog弹框和其背景阴影显示方法

 更新时间:2018年07月28日 16:58:14   作者:星旅zl  
今天小编就为大家分享一篇自定义Dialog弹框和其背景阴影显示方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

昨天研究了一下自定义Dialog的弹框,其实要点都是把自定义好的view用setContentView(view)的方法设置进dialog里,首先我们先看一个简单的自定义Dialog。

一、写布局文件:custom_dialog_layout.xml(这个布局就是一个简单的提示内容,下面有一个确定的按钮,请参看评论中的效果图)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/content_layout"
 android:layout_gravity="center"
 android:gravity="center">
 
 <LinearLayout
  android:background="@drawable/dialog_content_white_with_radius"
  android:layout_marginLeft="40dp"
  android:layout_marginRight="40dp"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:gravity="center">
  <TextView
   android:id="@+id/dialog_content_text"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="info"
   android:textSize="@dimen/size40"
   android:textColor="@color/word_color_444444"
   android:padding="10dp"
   android:gravity="center"/>
  <View
   android:layout_width="match_parent"
   android:layout_height="0.5dp"
   android:background="@color/divide_line"/>
  <TextView
   android:paddingTop="10dp"
   android:paddingBottom="10dp"
   android:id="@+id/tv_sure"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center"
   android:textColor="@color/main_color"
   android:text = "确定"
   android:textSize="@dimen/two_level_word"
   />
 </LinearLayout>
 
</LinearLayout>

写好布局文件后,由于布局直角不好看,我们可以设置边框为圆角的shape,写入,代码如下:dialog_content_white_with_radius

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 
 <solid android:color="@color/wirte_ffffff" />
 
 <corners
  android:bottomLeftRadius="8dp"
  android:bottomRightRadius="8dp"
  android:topLeftRadius="8dp"
  android:topRightRadius="8dp" />
</shape>

二、写自定义Dialog类继承自Dialog:

 /** [Description]
 * 只有确认button
 * [How to use]
 *
 * [Tips]
 *
 * Created by lan.zheng on 2017/7/25 18:26.
 */
 
public class SureClickDialog extends Dialog {
 private Listener mListener;
 
 public SureClickDialog(Context context) {
  super(context);
 }
 
 public SureClickDialog(Context context, String content, Listener listener){
  super(context, R.style.custom_dialog_style);
  mListener = listener;
  View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_have_been_sign_section_show, null);
  TextView contentTextView = (TextView) contentView.findViewById(R.id.dialog_content_text);
  contentTextView.setText(content);
  TextView sureButton = (TextView) contentView.findViewById(R.id.tv_sure);
 
  //消失监听
  this.setOnDismissListener(new OnDismissListener() {
   @Override
   public void onDismiss(DialogInterface dialog) {
    mListener.onDialogDismissListener();
   }
  });
  //确认
  sureButton.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    dismiss();
    mListener.onSureListerner();
   }
  });
  setContentView(contentView);
 }
 
 public interface Listener {
  void onDialogDismissListener();
  void onSureListerner();
 }
}

这里我们只监听弹框消失和点击确定的按钮,好了基本工作到这里完成了,最后就是设置样式style,一般弹框背景都是半透明的遮罩:

 <style name="custom_dialog_style" parent="android:Theme.Dialog">
  <item name="android:windowFrame">@null</item>
  <item name="android:windowIsFloating">true</item>
  <item name="android:windowIsTranslucent">false</item>
  <item name="android:windowNoTitle">true</item><!--除去title-->
  <item name="android:backgroundDimEnabled">true</item><!--半透明-->
  <item name="android:windowBackground">@color/transparent</item><!--除去背景色-->
  <item name="android:radius">10dp</item>
 </style>

这样就完成了一个背景半透明的弹框了。

设置<itemname="android:backgroundDimEnabled">true</item><!--半透明-->能实现半透明,但是如果有特殊的背景要求那就不能满足了,此时通过查询发现,可以重写下面这个函数进行把整个你自定义的布局全屏显示。

@Override
 public void show() {
  super.show();
  /**
   * 设置宽度全屏,要设置在show的后面
   */
  WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
  layoutParams.width= ViewGroup.LayoutParams.MATCH_PARENT;
  layoutParams.height= ViewGroup.LayoutParams.MATCH_PARENT;
  getWindow().getDecorView().setPadding(0, 0, 0, 0);
  getWindow().setAttributes(layoutParams);
 }

发现是生效的,我布局中的背景是成功的应用上了,但是发现点击外围却不能让弹框消失,这是因为你的弹框已经是全屏了,所以在屏幕上就没有所谓的弹框外围了,这时候我们可以自己去监听点击事件,我们来重写一下自定义Dialog类:

/**
 * [Description]
 * 只有确认button
 * [How to use]
 * new SureClickDialog()
 * [Tips]
 * isClickOutsideCanDismiss必须给值,true表示可点击外围消失,false表示不能
 * Created by lan.zheng on 2017/7/25 18:26.
 */
 
public class SureClickDialog extends Dialog {
 private Listener mListener;
 
 public SureClickDialog(Context context) {
  super(context);
 }
 
 public SureClickDialog(Context context, String content, boolean isClickOutsideCanDismiss,Listener listener){
  super(context, R.style.custom_dialog_style);
  mListener = listener;
  View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_have_been_sign_section_show, null);
  LinearLayout linearLayout = (LinearLayout)contentView.findViewById(R.id.content_layout) ; //自定义布局的最外层
  linearLayout.setBackgroundColor(context.getResources().getColor(R.color.half_transparent));
  linearLayout.setOnClickListener(new View.OnClickListener() { //为其设置自定义点击监听
   @Override
   public void onClick(View v) {
    if(isClickOutsideCanDismiss){
     dismiss();
    }
   }
  });
  TextView contentTextView = (TextView) contentView.findViewById(R.id.dialog_content_text);
  contentTextView.setText(content);
  TextView sureButton = (TextView) contentView.findViewById(R.id.tv_sure);
 
  //消失监听
  this.setOnDismissListener(new OnDismissListener() {
   @Override
   public void onDismiss(DialogInterface dialog) {
    mListener.onDialogDismissListener();
   }
  });
  //确认
  sureButton.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    dismiss();
    mListener.onSureListerner();
   }
  });
  setContentView(contentView);
 }
 
  @Override
 public void show() {
  super.show();
 
  WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
  layoutParams.width= ViewGroup.LayoutParams.MATCH_PARENT;
  layoutParams.height= ViewGroup.LayoutParams.MATCH_PARENT;
  getWindow().getDecorView().setPadding(0, 0, 0, 0);
  getWindow().setAttributes(layoutParams);
 }
 
 
 public interface Listener {
  void onDialogDismissListener();
  void onSureListerner();
 }
}

OK,关于弹框的就写到这里啦,自定义的功能十分丰富和具有可塑性,有兴趣的可以研究一下。

以上这篇自定义Dialog弹框和其背景阴影显示方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Android编程判断SD卡是否存在及使用容量查询实现方法

    Android编程判断SD卡是否存在及使用容量查询实现方法

    这篇文章主要介绍了Android编程判断SD卡是否存在及使用容量查询实现方法,实例分析了Android针对SD卡是否存在及使用容量、全部容量等的判断技巧,非常简单实用,需要的朋友可以参考下
    2015-10-10
  • android加载系统相册图片并显示详解

    android加载系统相册图片并显示详解

    大家好,本篇文章主要讲的是android加载系统相册图片并显示详解,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12
  • Flutter事件监听与EventBus事件的应用详解

    Flutter事件监听与EventBus事件的应用详解

    EventBus的核心是基于Streams。它允许侦听器订阅事件并允许发布者触发事件,使得不同组件的数据不需要一层层传递,可以直接通过EventBus实现跨组件通讯
    2023-04-04
  • Android自定义View实现比赛时间闪动效果

    Android自定义View实现比赛时间闪动效果

    这篇文章主要为大家详细介绍了Android自定义View实现比赛时间闪动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • Android 中使用RecyclerView实现底部翻页

    Android 中使用RecyclerView实现底部翻页

    这篇文章主要介绍了Android 中使用RecyclerView实现底部翻页功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-11-11
  • Android中实现长按修改ListView对象的内容

    Android中实现长按修改ListView对象的内容

    这篇文章主要给大家介绍了在Android中实现长按修改ListView对象内容的相关资料,文中给出了完整的示例代码,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-02-02
  • Android编程之绘图canvas基本用法示例

    Android编程之绘图canvas基本用法示例

    这篇文章主要介绍了Android编程之绘图canvas基本用法,结合实例形式分析了Android canvas绘图的相关方法与使用技巧,需要的朋友可以参考下
    2017-09-09
  • android studio 3.6.0 绑定视图新特性的方法

    android studio 3.6.0 绑定视图新特性的方法

    这篇文章主要介绍了android studio 3.6.0 绑定视图新特性的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04
  • Flutter + Idea 环境搭建及配置教程

    Flutter + Idea 环境搭建及配置教程

    本文主要总结我实际搭建的过程,最后发现不一定按网上那些博客或者官方文档写的来也可以搭建成功,在这里小编给大家分享下Flutter + Idea 环境搭建及配置教程,感兴趣的朋友参考下吧
    2021-12-12
  • android 实现按钮浮动在键盘上方的实例代码

    android 实现按钮浮动在键盘上方的实例代码

    这篇文章主要介绍了android 实现按钮浮动在键盘上方,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-03-03

最新评论