Android自定义Dialog及其布局
实际项目开发中默认的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及其布局的更多相关文章
- Android自定义 Dialog 对话框
Android自定义Dialoghttp://www.cnblogs.com/and_he/archive/2011/09/16/2178716.html Android使用自定义AlertDialo ...
- android自定义dialog布局
dialog使用系统自带的有时候不是很美观,就想要自己来设计一个dialog界面,以下就是可以设计的dialog界面: public class CustomDialog extends Dialog ...
- Android—自定义Dialog
在 Android 日常的开发中,Dialog 使用是比较广泛的.无论是提示一个提示语,还是确认信息,还是有一定交互的(弹出验证码,输入账号密码登录等等)对话框. 而我们去看一下原生的对话框,虽然随着 ...
- Android自定义Dialog(美化界面)
前言:在做项目的时候,发现dialog界面太丑陋,从csdn上下载了一份自定义dialog的源码,在他的基础上对界面进行美化...有需要的朋友可以直接拿走 效果图如下: 主要代码: /** * 自定义 ...
- android 自定义Dialog背景透明及显示位置设置
先贴一下显示效果图,仅作参考: 代码如下: 1.自定义Dialog public class SelectDialog extends AlertDialog{ public SelectDialog ...
- Android自定义Dialog
Android开发过程中,常常会遇到一些需求场景——在界面上弹出一个弹框,对用户进行提醒并让用户进行某些选择性的操作, 如退出登录时的弹窗,让用户选择“退出”还是“取消”等操作. Android系统提 ...
- Android 自定义Dialog类,并在Activity中实现按钮监听。
实际开发中,经常会用到Dialog,比如退出时候会弹出是否退出,或者还有一些编辑框也会用Dialog实现,效果图如下: 开发中遇到的问题无非在于如果在Activity中监听这个Dialog中实现的 ...
- android 自定义Dialog去除黑色边框
在自定义Dialog时显示的界面中老是有黑色的边框,下面就介绍使用style去除黑色边框方法. 首先在values/styles定义自定义样式: <style name="MyDial ...
- Android 自定义Dialog 去除阴影
自定义Dialog中添加下列代码: window.clearFlags( WindowManager.LayoutParams.FLAG_DIM_BEHIND);
随机推荐
- js禁止重复提交方法
beforeSend: function () { // 禁用按钮防止重复提交 $("#fileForm a[class='btn blue_btn']").removeAttr( ...
- JavaScript 基础第一天
一.前言 如果将前端比喻成一人,那么Html就是这个人的骨骼脉络,CSS则是这个人的身上的装饰,JavaScript则是这个人的行为.作为一种脚本语言JavasSript具有着弱类型等特点.(在这里我 ...
- Java使用velocity导出word
效果展示: 使用word编辑好模板
- ABP框架详解(五)Navigation
ABP框架中的Navigation功能用于管理业务系统中所有可用的菜单导航控件,通常在业务系统的首页会有一个全局性的导航菜单,JD商城,天猫,猪八戒网莫不如是.所以为方便起见,Navigation功能 ...
- .net 第二周学习
这周更进一步的介绍了.net,通过作业的练习,有那么一点点的成就感,相对于前端,成就感还不是很大,但是我还是会继续加油 学习.net,看着他们周末只能呆在寝室写网页,顿时我就高兴了: ...
- 重制AdvanceWars第一步 -- 搞定地图
首先来聊下高级战争吧Advance Wars,由任天堂旗下的Intelligent Systems开发的战棋游戏.初作诞生于GBA上,后来继续跟进了高战2黑洞崛,而后在下一代掌机DS上也出了三代续作高 ...
- Sql Server tempdb原理-缓存机制解析实践
Tempdb就像Sqlserver的临时仓库,各式各样的对象,数据在里面进行频繁计算,操作.大量的操作使得tempdb可能面临很大压力,tempdb中缓存的设计就是为了缓解这些压力.这次就为大家介绍下 ...
- python 对象
python 对象 在python中,对象就是为C中的结构体在堆上申请的一块内存,一般来说,对象是不能被静态初始化的,并且不能再栈空间上生存.本文主要对Python的基本数据类型做简单的介绍. PyO ...
- 使用后缀数组寻找最长公共子字符串JavaScript版
后缀数组很久很久以前就出现了,具体的概念读者自行搜索,小菜仅略知一二,不便讨论. 本文通过寻找两个字符串的最长公共子字符串,演示了后缀数组的经典应用. 首先需要说明,小菜实现的这个后缀数组算法,并非标 ...
- Git学习笔记(1)——安装,配置,创建库,文件添加到库
初次接触git,为了记忆深刻,把学习的简单流程记录下来. 本文记录了Git在Ubuntu上的安装,配置,以及创建版本库和往库中添加文件的过程. 1.Git的安装:(Ubuntu-Linux非常友好的安 ...