Android 自定义AlertDialog对话框
2019独角兽企业重金招聘Python工程师标准>>>
系统默认的AlertDialog,与项目的UI不统一,所以,改了一下,定义了一样式,最终效果如下图:
另外,为了尽量少改原来的代码,该类类名及相关方法名都与android.app.AlertDialog相同,使用时,原代码只需要修改导入的包名,在按钮的Listener上加上一句关闭对话框的方法即可.
先是对话框的XML布局文件:
test.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- 顶部椭园边缘 --><ImageView android:layout_width="400dp" android:layout_height="22dp" android:src="@drawable/dialog_top" ></ImageView><!-- 中间白色背景,两个TextView,标题和内容,留一个LinearLayout,在代码中根据调用动态加上按钮 --><LinearLayout android:layout_width="400dp" android:layout_height="wrap_content" android:background="@drawable/dialog_center" android:orientation="vertical" > <TextView android:textColor="#000000" android:textSize="35dp" android:gravity="center" android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:layout_marginLeft="20dp" android:layout_marginRight="10dp" android:id="@+id/message" android:layout_marginBottom="10dp" android:layout_marginTop="20dp" android:textColor="#000000" android:textSize="25dp" android:layout_width="fill_parent" android:layout_height="wrap_content" /><!-- 在LinearLayout中加按钮 --><LinearLayout android:id="@+id/buttonLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:gravity="center" android:orientation="horizontal" > </LinearLayout></LinearLayout><!-- 底部椭园边缘 --><ImageView android:layout_marginTop="-2dp" android:layout_width="400dp" android:layout_height="22dp" android:src="@drawable/dialog_bottom" ></ImageView> </LinearLayout>
按钮背景,不同状态不同,可参考本博相关文章:
alertdialog_button.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" ><item android:state_pressed="true" android:drawable="@drawable/dialog_button_pressed" /><item android:state_focused="true" android:drawable="@drawable/dialog_button_pressed" /><item android:drawable="@drawable/dialog_button" /></selector>
下面就是自定义的AlertDialog类的代码:
public class AlertDialog {Context context;android.app.AlertDialog ad;TextView titleView;TextView messageView;LinearLayout buttonLayout;public AlertDialog(Context context) {// TODO Auto-generated constructor stubthis.context=context;ad=new android.app.AlertDialog.Builder(context).create();ad.show();//关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局Window window = ad.getWindow();window.setContentView(R.layout.alertdialog);titleView=(TextView)window.findViewById(R.id.title);messageView=(TextView)window.findViewById(R.id.message);buttonLayout=(LinearLayout)window.findViewById(R.id.buttonLayout);}public void setTitle(int resId){titleView.setText(resId);}public void setTitle(String title) {titleView.setText(title);}public void setMessage(int resId) {messageView.setText(resId);} public void setMessage(String message){messageView.setText(message);}/*** 设置按钮* @param text* @param listener*/public void setPositiveButton(String text,final View.OnClickListener listener){Button button=new Button(context);LinearLayout.LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);button.setLayoutParams(params);button.setBackgroundResource(R.drawable.alertdialog_button);button.setText(text);button.setTextColor(Color.WHITE);button.setTextSize(20);button.setOnClickListener(listener);buttonLayout.addView(button);} /*** 设置按钮* @param text* @param listener*/public void setNegativeButton(String text,final View.OnClickListener listener){Button button=new Button(context);LinearLayout.LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);button.setLayoutParams(params);button.setBackgroundResource(R.drawable.alertdialog_button);button.setText(text);button.setTextColor(Color.WHITE);button.setTextSize(20);button.setOnClickListener(listener);if(buttonLayout.getChildCount()>0){params.setMargins(20, 0, 0, 0);button.setLayoutParams(params);buttonLayout.addView(button, 1);}else{button.setLayoutParams(params);buttonLayout.addView(button);} }/*** 关闭对话框*/public void dismiss() {ad.dismiss();} }
使用方法:
final AlertDialog ad=new AlertDialog(Test.this);
ad.setTitle("标题");
ad.setMessage("内容sdfsafdasf内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容");
ad.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(View v) {// TODO Auto-generated method stubad.dismiss();Toast.makeText(Test.this, "被点到确定", Toast.LENGTH_LONG).show();
}
});
ad.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ad.dismiss();
Toast.makeText(Test.this, "被点到取消", Toast.LENGTH_LONG).show();
}
});
转载于:https://my.oschina.net/u/1036767/blog/266318
Android 自定义AlertDialog对话框相关推荐
- android自定义dialog对话框,android的自定义dialog对话框实现
Android自定义dialog对话框 一:自定义dialog 如何填充布局 ①自定MyDialog类,在类里关联布局 public class MyDialog extends AlertDialo ...
- Android自定义AlertDialog的控件获取操作
Android自定义AlertDialog的控件获取操作 在自定义的AlertDialog布局虽然可以显示,但是试过很多方法都不能获得其中的控件进行操作,找了很多方法最后这种方法可以. dialog的 ...
- 024 Android 自定义样式对话框(AlertDialog)
1.AlertDialog介绍 AlertDialog并不需要到布局文件中创建,而是在代码中通过构造器(AlertDialog.Builder)来构造标题.图标和按钮等内容的. 常规使用步骤(具体参见 ...
- Android自定义AlertDialog
常见的一种方法: [html] view plaincopyprint? AlertDialog.Builder builder; AlertDialog alertDialog; LayoutInf ...
- Android 自定义AlertDialog类
还是先从最简单的开始吧,然后一步一步的扩展. 为了保证软件上所谓的低耦合度和可重用性,这里不得不需要单独建立一个类CustomerDialog,然后继承AlertDialog public class ...
- android activity传值到dialog,android 自定义AlertDialog 与Activity相互传递数据
**主要实现功能:** 1.从Activity的TextView中获取字符串设置到AlertDialog的TextView和EditText中 2.将AlertDialog的EditText中的值设置 ...
- Android 自定义AlertDialog
Android 自定义popuWindow 对于项目需求中,系统的弹出框不能满足我们的需求,就只能通过自定义布局来实现自己相对应的功能,这里总结一篇关于AlertDialog的 具体操作 这里把弹出框 ...
- Android 自定义AlertDialog,调用方法与系统一致
2019独角兽企业重金招聘Python工程师标准>>> 由于android原生的AlertDialog都一致,有时为了和你的项目的Dialog保持一致,你最先想到的就是有没有Aler ...
- Android自定义一个对话框,完全自定义Android对话框AlertDialog的实现
Android本身封装的AlertDialog.Builder很方便易用,但如果想要自定义弹出对话框的风格,如标题字体背景元素间隔之类的,那就比较困难了. 最近我就遇到了这个问题,一个工程的界面风格全 ...
最新文章
- 谷歌大改Transformer注意力,速度、内存利用率都提上去了
- 配合使用自制的PE3.0启动盘和Windows部署服务,实现Ghost网克
- android5.0以后获取应用运行状态,Android判断App前台运行还是后台运行(运行状态)...
- IntelliJ IDEA 显示行号方法
- [Pro*c]滚动游标变量的使用
- 整理一篇Linux drm显示系统的文章
- 设置按钮中的图片的旋转,并且旋转之后不变形
- python正则匹配空格+数字+空格_详解Python中正则匹配TAB及空格的小技巧
- 背靠福特的Argo无人车发生严重事故,两名乘客已送往医院
- ORB_SLAM2探秘 第三章 LoopClosing线程
- jtree和mysql_让JTree所有节点展开和关闭的类 - 我爱看火影的日志 - 网易博客
- Sketch for Mac汉化破解教程含汉化包
- 找出01二维矩阵中只包含 1 的最大正方形,并输出其面积_java
- C语言水电管理系统,小区水电费管理系统C语言.doc
- linux之进程观察命令:ps和top
- echart 柱状图 ---- 坐标轴、网格、柱体配置
- Cocos Creator 优化,帧动画优化
- 有哪些好用的视频录制软件?快进来学习一波
- 【分享】vofa+串口调试助手
- 论文翻译:Text-based Image Editing for Food Images with CLIP
热门文章
- 专栏 | IBM Watson启示录:AI不应该仅仅是炫技
- 美国喜提刷脸登机,官方开心发通告,竟马上引发公民不适
- 被外包程序员植入了后门程序,触发后删除数据库但他们死不承认,该怎么办?...
- Linux下kvm:检测硬件是否支持虚拟化
- linux主机名包含点
- 如何让Android手机远离间谍软件?看这里
- 切实把握大数据时代的新机遇新变革
- windows 2012 非pdc DC强行重置NTP
- maven-dependency-plugin (goals copy-dependencies, unpack) is not support解决办法
- 关于二级域名Cookie的问题及解决方法