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);
随机推荐
- iOS7界面的兼容性调整之一
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { self.edgesForExtendedLayout ...
- 关于Android的背景色配色小结
三基色原理:三基色是指红,绿,蓝三色,人眼对红.绿.蓝最为敏感,大多数的颜色可以通过红.绿.蓝三色按照不同的比例合成产生.同样绝大多数单色光也可以分解成红绿蓝三种色光.这是色度学的最基本原理,即三基色 ...
- 搭建coreseek(sphinx+mmseg3)详细安装配置+php之sphinx扩展安装+php调用示例
http://blog.csdn.net/e421083458/article/details/21529969 常用的命令 ps -ef|grep searchd 如果你开了search服务后,你命 ...
- .Net MVC中的路由
MVC中的URL也就是路由,不用通过路径访问实际的物理文件而达到向用户展示界面. URL的路由功能 一方面正向 映射到Controller和Action,根据用户输入的URL触发相应的控制器和对应方法 ...
- 反射实现 AOP 动态代理模式(Spring AOP 的实现 原理)
好长时间没有用过Spring了. 突然拿起书.我都发现自己对AOP都不熟悉了. 其实AOP的意思就是面向切面编程. OO注重的是我们解决问题的方法(封装成Method),而AOP注重的是许多解决解决问 ...
- log4net入门
简介 几乎所有的大型应用都会有自己的用于跟踪调试的API.因为一旦程序被部署以后,就不太可能再利用专门的调试工具了.然而一个管理员可能需要有一套强大的日志系统来诊断和修复配置上的问题. 经验表明,日志 ...
- Snmp协议应用-扫描局域网内打印机
.h2cls { background: #6fa833 none repeat scroll 0 0 !important; color: #fff; font-family: "微软雅黑 ...
- RCP: JDT 根据org.eclipse.jdt.core.IJavaElement对象获取org.eclipse.jdt.core.dom.ASTNode对象
JDT中有两套Java文件模型映射. 其核心类\接口分别为: org.eclipse.jdt.core.IJavaElement和org.eclipse.jdt.core.dom.ASTNode IJ ...
- Jenkins+Git 集成测试(build、zip、curl)
自上篇文章<jenkins + Git 搭建持续集成环境>后,这次配置的job有了新的要求,同时也踩到了新的坑.特此记录,以警己身. 任务要求: 这三个步骤都在job配置页面中Build部 ...
- 《C#图解教程》读书笔记之五:委托和事件
本篇已收录至<C#图解教程>读书笔记目录贴,点击访问该目录可获取更多内容. 一.委托初窥:一个拥有方法的对象 (1)本质:持有一个或多个方法的对象:委托和典型的对象不同,执行委托实际上是执 ...