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

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

昨天研究了一下自定义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弹框和其背景阴影显示方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享

TextView显示系统时间(时钟功能带秒针变化

用System.currentTimeMillis()可以获取系统当前的时间,我们可以开启一个线程,然后通过handler发消息,来实时的更新TextView上显示的系统时间,可以做一个时钟的功能
收藏 0 赞 0 分享

Android用ListView显示SDCard文件列表的小例子

本文简单实现了用ListView显示SDCard文件列表,目录的回退等功能暂不讨论,获取文件列表,files即为所选择目录下的所有文件列表
收藏 0 赞 0 分享

Android拦截外拨电话程序示例

这篇文章主要介绍了Android拦截外拨电话的示例,大家参考使用吧
收藏 0 赞 0 分享

通过Html网页调用本地安卓(android)app程序代码

如何使用html网页和本地app进行传递数据呢?经过研究,发现还是有方法的,总结了一下,大致有一下几种方式
收藏 0 赞 0 分享

android Textview文字监控(Textview使用方法)

以手机号充值为例,当用户输入最后一位数时候,进行汇率的变换,本文就实现类似这样的功能
收藏 0 赞 0 分享

Android ListView长按弹出菜单二种实现方式示例

这篇文章主要介绍了Android ListView长按弹出菜单的方法,大家参考实现
收藏 0 赞 0 分享
查看更多