安卓—自定义 AlertDialog 的样式
自定义修改安卓弹出框的样式
效果图:

1.在style.xml下添加
<!-- 自定义弹出样式 -->
<style name="MyDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<!--是否浮在窗口之上-->
<!--<item name="android:windowIsFloating">true</item>-->
<!--半透明-->
<!--<item name="android:windowIsTranslucent">true</item>-->
<!--是否显示title-->
<item name="android:windowNoTitle">true</item>
<!--dialog之外没有焦点的区域是否罩上黑色半透明-->
<item name="android:background">@color/white</item>
<item name="android:textColor">@color/gray</item>
<item name="android:textSize">@dimen/f24</item>
<!-- 这里是修改顶部标题背景颜色,具体颜色自己定,可以是图片 -->
<item name="android:topDark">@color/blue_alert</item>
<!-- 这里是修改内容区域背景颜色 -->
<item name="android:layout_gravity">center</item>
<item name="buttonBarButtonStyle">@color/white</item>
</style>
2.在主体配置里引入自定义的AlertDialog主题

3.java代码写法
/**
* 默认的弹出窗口
* @param view
*/
public void alertDialog(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("弹出窗");
builder.setMessage("提示信息!");
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
System.out.println("确认执行函数");
}
}).setNegativeButton("取消", null);
builder.show();
} /**
* 自定义样式的弹出窗
* @param view
*/
public void alertDialog2(View view) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
// 自定义 title样式
TextView title = new TextView(this);
title.setText("自定义弹出窗");
title.setTextSize(24);
title.setGravity(Gravity.CENTER);
title.setPadding(0,20,0,20);
builder.setCustomTitle(title);
// 中间的信息以一个view的形式设置进去
TextView msg = new TextView(this);
msg.setText("自定义弹出提示信息");
msg.setTextSize(24);
msg.setGravity(Gravity.CENTER);
msg.setPadding(20, 40, 20, 40);
builder.setView(msg); builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
System.out.println("确认执行函数");
}
}).setNegativeButton("取消", null);
// 调用 show()方法后得到 dialog对象
AlertDialog dialog = builder.show();
final Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
final Button negativeButton=dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
LinearLayout.LayoutParams positiveParams =(LinearLayout.LayoutParams)positiveButton.getLayoutParams();
positiveParams.gravity = Gravity.CENTER;
positiveParams.setMargins(10,10,10,10);
positiveParams.width = 0;
// 安卓下面有三个位置的按钮,默认权重为 1,设置成 500或更大才能让两个按钮看起来均分
positiveParams.weight = 500;
LinearLayout.LayoutParams negativeParams =(LinearLayout.LayoutParams)negativeButton.getLayoutParams();
negativeParams.gravity = Gravity.CENTER;
negativeParams.setMargins(10,10,10,10);
negativeParams.width = 0;
negativeParams.weight = 500;
positiveButton.setLayoutParams(positiveParams);
negativeButton.setLayoutParams(negativeParams);
positiveButton.setBackgroundColor(Color.parseColor("#FF733E"));
positiveButton.setTextColor(Color.parseColor("#FFFFFF"));
negativeButton.setBackgroundColor(Color.parseColor("#DDDDDD"));
}
安卓—自定义 AlertDialog 的样式的更多相关文章
- 安卓 自定义AlertDialog对话框(加载提示框)
AlertDialog有以下六种使用方法: 一.简单的AlertDialog(只显示一段简单的信息) 二.带按钮的AlertDialog(显示提示信息,让用户操作) 三.类似ListView的Aler ...
- 自定义AlertDialog的样式
一.在XML中定义好要显示的AlertDialog的布局 二.在代码中创建alertdialog 对象 AlertDialog dialog = new AlertDialog.Builder(thi ...
- 自定义AlertDialog(仿微信)
安卓自定义AlertDialog,原理很简单: AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).create(); di ...
- AlertDialog.Builder 样式设置
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDia ...
- jQuery Validate 表单验证插件----自定义校验结果样式
一.下载依赖包 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW 访问密码 f224 二.引入依赖包 <script src="../../scripts/j ...
- Android 自定义RadioButton的样式
Android 自定义RadioButton的样式 我们知道Android控件里的button,listview可以用xml的样式自定义成自己希望的漂亮样式. 最近用到RadioButton,利用xm ...
- ActionBar官方教程(11)自定义ActionBar的样式(含重要的样式属性表及练习示例)
Styling the Action Bar If you want to implement a visual design that represents your app's brand, th ...
- 自定义input file样式
自定义input file样式:一般都是通过隐藏input,通过定义label来实现.这种做法要注意的是label的for属性要指定input对应的id; <!DOCTYPE html> ...
- WPF界面设计技巧(4)—自定义列表项样式
原文:WPF界面设计技巧(4)-自定义列表项样式 有前面修改按钮样式的基础,我们可以尝试来定制一个即好看又好用的 ListBox ,今天先来讲“好看”部分. 打开 Microsoft Visual S ...
随机推荐
- Git基础概念与Flow流程介绍
目录 Git相关 基本概念 常见客户端 TortoiseGit Sourcetree Intellij Idea 命令行 常用命令 存储区域 命令之 add & commit &pus ...
- Redis Cluster 原理相关说明
背景 之前写的 Redis Cluster部署.管理和测试 和 Redis 5.0 redis-cli --cluster help说明 已经比较详细的介绍了如何安装和维护Cluster.但关于Clu ...
- MongoDB 学习笔记之 group聚合
group聚合: key: 分组字段 cond:过滤条件 reduce: curr是当前行 result是每组的结果集 initial : 组变量初始值 finalize: 统计一组后的回调函数 用g ...
- java架构之路-(分布式zookeeper)zookeeper真实使用场景
上几次博客,我说了一下Zookeeper的简单使用和API的使用,我们接下来看一下他的真实场景. 一.分布式集群管理✨✨✨ 我们现在有这样一个需求,请先抛开Zookeeper是集群还是单机的概念,下面 ...
- Spring Boot (十三): Spring Boot 整合 RabbitMQ
1. 前言 RabbitMQ 是一个消息队列,说到消息队列,大家可能多多少少有听过,它主要的功能是用来实现应用服务的异步与解耦,同时也能起到削峰填谷.消息分发的作用. 消息队列在比较主要的一个作用是用 ...
- spring boot通过Jedis来操作redis
idea中新建spring boot项目,引入jedis依赖 <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> ...
- IntelliJ IDEA使用JUnit单元测试
转载:https://www.cnblogs.com/huaxingtianxia/p/5563111.html 前言 单元测试的基本使用 一.环境配置 使用idea IDE 进行单元测试,首先需要安 ...
- BS结构的一个注册用户的功能
注册用户功能 学了Java一段时间,就想折腾折腾,就做了一个注册的功能,用HTML写了一个网页上的比较简陋的界面,用Java做了一个后台简陋的服务器处理数据,最后将数据存储到数据库中. 注册界面 ...
- CS231n:Python Numpy教程
Python 基本数据类型 容器 列表 字典 集合 元组 函数 类 Numpy 数组 访问数组 数据类型 数组计算 广播 SciPy 图像操作 MATLAB文件 点之间的距离 Matplotlib 绘 ...
- 微信小程序学习总结
微信小程序开发环境安装以及相关设置配置 微信小程序前端页面书写 微信小程序前端样式WXSS书写 微信小程序中事件 微信小程序自定义组件 微信小程序发起请求 微信小程序登入流程 微信小程序路由跳转 微信 ...