实际项目开发中默认的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. node.js问题二

    看了Node.js开发指南发现routes和app.js分开的话要使用下面代码 app.use(express.router(routes)) 但是真正是使用上面代码会遇到无数的问题报错 找了资料才发 ...

  2. node(规则引擎)

    本文主要记录node的下的一个开源规则引擎nools,给出简单的实例,github地址为: https://github.com/C2FO/nools 定义规则引擎(test.nools) defin ...

  3. STC12C5A60S2笔记1(管脚定义)

    STC12C5A60S2管脚定义 管脚1:标准IO口P1.0.ADC0 模数转换通道0.CLKOUT2 波特率发生器的时钟输出 管脚2:标准IO口P1.1.ADC1 模数转换通道1 管脚3:标准IO口 ...

  4. AMD加载器实现笔记(二)

    AMD加载器实现笔记(一)中,我们实现了一个简易的模块加载器.但到目前为止这个加载器还并不能称为AMD加载器,原因很简单,我们还不支持AMD规范中的config配置.这篇文章中我们来添加对config ...

  5. WCF基础教程之异常处理:你的Try..Catch语句真的能捕获到异常吗?

    在上一篇WCF基础教程之开篇:创建.测试和调用WCF博客中,我们简单的介绍了如何创建一个WCF服务并调用这个服务.其实,上一篇博客主要是为了今天这篇博客做铺垫,考虑到网上大多数WCF教程都是从基础讲起 ...

  6. 使用mybatis访问sql server

    原创文章转载请注明出处:@协思, http://zeeman.cnblogs.com mybatis作为一种半自动化的ORM工具,可以提供更大的灵活性,逐渐受到社区的欢迎. 官方下载地址是:https ...

  7. mysqlnd cannot connect to MySQL 4.1+

    phpMyAdmin - error #2000 - mysqlnd cannot connect to MySQL 4.1+ using the old insecure authenticatio ...

  8. 自制Unity小游戏TankHero-2D(1)制作主角坦克

    自制Unity小游戏TankHero-2D(1)制作主角坦克 我在做这样一个坦克游戏,是仿照(http://game.kid.qq.com/a/20140221/028931.htm)这个游戏制作的. ...

  9. AWS系列之三 使用EBS

    Amazon Elastic Block Store(EBS)可作为EC2实例的持久性数据块级存储.其具有高可用性和持久性的特点,可用性高达99.999%.给现有的EC2实例扩展新的存储块只需要几分钟 ...

  10. mac命令

    mac下卸载nodesudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man/*/node.*}xc ...