实际项目开发中默认的Dialog样式无法满足需求,需要自定义Dialog及其布局,并响应布局中控件的事件。

上效果图:

自定义Dialog,LogoutDialog:

要将自定义布局传入构造函数中,才能在Activity中通过 dialog.findviewbyid 获取到控件,否则返回null。

public class LogoutDialog extends Dialog{
  Context context;
  public LogoutDialog(Context context) {
  super(context);
  this.context=context;
  this.setContentView(R.layout.logout_dialog);
  }   public LogoutDialog(Context context, int theme){
  super(context, theme);
  this.context = context;
  this.setContentView(R.layout.logout_dialog);
  }   @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //this.setContentView(R.layout.logout_dialog);
  }
}

自定义布局文件,logout_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/transparent"
>   <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@drawable/dialog_top_bg"
  android:orientation="vertical"
  android:paddingTop="10dp"
  >
    <ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:src="@drawable/pop_icon1"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="10dp"
    />
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="是否确认注销账号"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="20dp"
    android:textColor="#FF3C25"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    />
  </LinearLayout>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:gravity="center_vertical"
    android:background="@drawable/dialog_buttom_bg"
    >
    <TextView
    android:id="@+id/back_btn_dialog"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="返回"
    android:layout_weight="1"
    android:textColor="#FFFFFF"
    android:gravity="center"
    />
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="|"
    android:textColor="#C9CACC"
    android:gravity="center"
    />
    <TextView
    android:id="@+id/submit_btn_dialog"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="确定"
    android:layout_weight="1"
    android:textColor="#FFFFFF"
    android:gravity="center"
    />
  </LinearLayout> </LinearLayout>

两个Shape的布局,dialog_buttom_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
>
  <corners
  android:bottomLeftRadius="@dimen/dialog_corners"
  android:bottomRightRadius="@dimen/dialog_corners"
  />
  <gradient
  android:startColor="#FF3E25"
  android:endColor="#FF3E25"
  android:centerColor="#FF3E25"
  android:angle="270"
  />
</shape>

dialog_top_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <corners
  android:topLeftRadius="@dimen/dialog_corners"
  android:topRightRadius="@dimen/dialog_corners"
  />
  <gradient
  android:startColor="#ffffff"
  android:endColor="#ffffff"
  android:centerColor="#ffffff"
  android:angle="270"
  />
</shape>

自定义样式Style,来改变默认的Dialog样式。在values/styles.xml下新加样式:

<style name="UpdateErrorFinishDialog" parent="@android:style/Theme.Dialog">
  <item name="android:windowFrame">@null</item>
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowBackground">@drawable/dialog_background</item>
  <item name="android:windowIsFloating">true</item>
  <item name="android:windowContentOverlay">@null</item>
</style>

在Activity中调用:

Dialog dialog==new LogoutDialog(TempActivity.this, R.style.UpdateErrorFinishDialog);

dialog.setCanceledOnTouchOutside(false);
dialog.show(); TextView submit_btn_dialog=(TextView) dialog.findViewById(R.id.submit_btn_dialog);
TextView back_btn_dialog=(TextView) dialog.findViewById(R.id.back_btn_dialog);
submit_btn_dialog.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    //Toaster.showLongToast("确定");
    dialog.dismiss();
  }
});
back_btn_dialog.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    //Toaster.showLongToast("返回");
    dialog.dismiss();
  }
});

Android自定义Dialog及其布局的更多相关文章

  1. Android自定义 Dialog 对话框

    Android自定义Dialoghttp://www.cnblogs.com/and_he/archive/2011/09/16/2178716.html Android使用自定义AlertDialo ...

  2. android自定义dialog布局

    dialog使用系统自带的有时候不是很美观,就想要自己来设计一个dialog界面,以下就是可以设计的dialog界面: public class CustomDialog extends Dialog ...

  3. Android—自定义Dialog

    在 Android 日常的开发中,Dialog 使用是比较广泛的.无论是提示一个提示语,还是确认信息,还是有一定交互的(弹出验证码,输入账号密码登录等等)对话框. 而我们去看一下原生的对话框,虽然随着 ...

  4. Android自定义Dialog(美化界面)

    前言:在做项目的时候,发现dialog界面太丑陋,从csdn上下载了一份自定义dialog的源码,在他的基础上对界面进行美化...有需要的朋友可以直接拿走 效果图如下: 主要代码: /** * 自定义 ...

  5. android 自定义Dialog背景透明及显示位置设置

    先贴一下显示效果图,仅作参考: 代码如下: 1.自定义Dialog public class SelectDialog extends AlertDialog{ public SelectDialog ...

  6. Android自定义Dialog

    Android开发过程中,常常会遇到一些需求场景——在界面上弹出一个弹框,对用户进行提醒并让用户进行某些选择性的操作, 如退出登录时的弹窗,让用户选择“退出”还是“取消”等操作. Android系统提 ...

  7. Android 自定义Dialog类,并在Activity中实现按钮监听。

      实际开发中,经常会用到Dialog,比如退出时候会弹出是否退出,或者还有一些编辑框也会用Dialog实现,效果图如下: 开发中遇到的问题无非在于如果在Activity中监听这个Dialog中实现的 ...

  8. android 自定义Dialog去除黑色边框

    在自定义Dialog时显示的界面中老是有黑色的边框,下面就介绍使用style去除黑色边框方法. 首先在values/styles定义自定义样式: <style name="MyDial ...

  9. Android 自定义Dialog 去除阴影

    自定义Dialog中添加下列代码: window.clearFlags( WindowManager.LayoutParams.FLAG_DIM_BEHIND);

随机推荐

  1. redmine中创建项目与跟踪标签(原创)

    今天来说下本公司所用到的项目管理工具redmine,总体来说还是比较好用的.redmine中可以记录项目的整个过程,可创建跟踪标签(里程碑.需求用例.功能.任务.缺陷)来进行对项目的管控.跟踪标签根据 ...

  2. Largest Rectangle in a Histogram(DP)

    Largest Rectangle in a Histogram Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K ...

  3. 【转】VC中的字符串处理

    http://hi.baidu.com/nmn714/item/ab8d2a96d0f2d6f228164727 貌似不少人刚开始做windows程序时都会纠结在字符串处理上,所以我把关于字符串处理的 ...

  4. angularjs http 请求拦截器

    /** * Created by oy on 2016/11/29. */ (function() { 'use strict'; // 创建angular模块 angular .module('ap ...

  5. 在WebApi中基于Owin OAuth使用授权发放Token

    如何基于Microsoft.Owin.Security.OAuth,使用Client Credentials Grant授权方式给客户端发放access token? Client Credentia ...

  6. DOM何时Ready

    由于script标签在被加载完成后会立即执行其中代码,如果在代码中要访问HTMLElement,可是这时候元素还没有加载进来,所以对元素的操作统统无效. 最早的时候使用window.onload = ...

  7. HOOK技术的一些简单总结

    好久没写博客了, 一个月一篇还是要尽量保证,今天谈下Hook技术. 在Window平台上开发任何稍微底层一点的东西,基本上都是Hook满天飞, 普通应用程序如此,安全软件更是如此, 这里简单记录一些常 ...

  8. IIS中发布网站的问题

    1.将网站发布到IIS,访问发生如下错误: HTTP 错误 500.21 - Internal Server Error处理程序"PageHandlerFactory-Integr" ...

  9. jQuery疑惑记录

    不断更新 1.项目中有一句 this.$html = $('<div/>').html(html).attr('sspa-mod-id', this.modName).hide();不知道 ...

  10. iOS 向模拟器里添加照片

    iOS 向模拟器里添加照片 模拟器里Photos最开始时是没有照片的,有时我们做Demo时需要Photos里面的照片做测试,这时就需要把Mac上的照片导入到模拟器里.步骤如下: 1,打开模拟器 2,选 ...