自定义修改安卓弹出框的样式

效果图:

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 的样式的更多相关文章

  1. 安卓 自定义AlertDialog对话框(加载提示框)

    AlertDialog有以下六种使用方法: 一.简单的AlertDialog(只显示一段简单的信息) 二.带按钮的AlertDialog(显示提示信息,让用户操作) 三.类似ListView的Aler ...

  2. 自定义AlertDialog的样式

    一.在XML中定义好要显示的AlertDialog的布局 二.在代码中创建alertdialog 对象 AlertDialog dialog = new AlertDialog.Builder(thi ...

  3. 自定义AlertDialog(仿微信)

    安卓自定义AlertDialog,原理很简单: AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).create(); di ...

  4. AlertDialog.Builder 样式设置

    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDia ...

  5. jQuery Validate 表单验证插件----自定义校验结果样式

    一.下载依赖包 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW  访问密码 f224 二.引入依赖包 <script src="../../scripts/j ...

  6. Android 自定义RadioButton的样式

    Android 自定义RadioButton的样式 我们知道Android控件里的button,listview可以用xml的样式自定义成自己希望的漂亮样式. 最近用到RadioButton,利用xm ...

  7. ActionBar官方教程(11)自定义ActionBar的样式(含重要的样式属性表及练习示例)

    Styling the Action Bar If you want to implement a visual design that represents your app's brand, th ...

  8. 自定义input file样式

    自定义input file样式:一般都是通过隐藏input,通过定义label来实现.这种做法要注意的是label的for属性要指定input对应的id; <!DOCTYPE html> ...

  9. WPF界面设计技巧(4)—自定义列表项样式

    原文:WPF界面设计技巧(4)-自定义列表项样式 有前面修改按钮样式的基础,我们可以尝试来定制一个即好看又好用的 ListBox ,今天先来讲“好看”部分. 打开 Microsoft Visual S ...

随机推荐

  1. 一个基于protobuf的极简RPC

    前言 RPC采用客户机/服务器模式实现两个进程之间的相互通信,socket是RPC经常采用的通信手段之一.当然,除了socket,RPC还有其他的通信方法:http.管道...网络开源的RPC框架也比 ...

  2. SpannableString与SpannableStringBuilder

    一.概述 1.SpannableString.SpannableStringBuilder与String的关系 首先SpannableString.SpannableStringBuilder基本上与 ...

  3. Docker 配置国内镜像

    前言 当我们使用Docker pull 拉取镜像时,有时候因网络问题,导致获取镜像报错,如下 :Error response from daemon:Get https://registry-1.co ...

  4. SQL server数据库系统部分常用的存储过程及说明

    --SQL server数据库系统常用的存储过程 exec sp_databases --能看到所有具有权限的数据库名,大小和备注 exec sp_helpdb --数据库名,大小,管理员,创建时间状 ...

  5. Jenkins部署(基于windows)

    一.安装jdk,配置环境变量 二.安装tomcat和jenkins 1.检查电脑上8080端口是否被占用: 命令行中输入:netstat -ano 2.下载Tomcat Tomcat官方网站:http ...

  6. sql数据文件导入数据库

    1.首先通过xshell连接数据库服务器,执行命令mysql -u root -p 命令,按照提示输入密码.连接上数据库. 2.在连接终端上执行命令create database JD_Model; ...

  7. SQLMAP SSI注入错误解决

    记一次SQL注入 目标地址:https://www.xxxx.com/ 之前补天提交过这个注入  后来貌似”修复了”(实际就是装了安全狗和过滤了一些关键字) 不过今天试了下 还是可以注入 可以看到已经 ...

  8. Git上传项目至GitHub

    要使用github,首先需要下载git.安装git 1.先在github上创建一个git仓库,复制该github路径 2.用cmd进入一个目录 git clone github路径 从github上直 ...

  9. #10056. 「一本通 2.3 练习 5」The XOR-longest Path

    题目描述 有一棵带权树,求这些边组成的路径的最大异或和. Solution 考场 SPFA 神奇 70 分代码 #include<cstdio> #include<cstdlib&g ...

  10. USACO环绕岛屿Surround the Islands 并查集 枚举暴力

    题目描述 Farmer John has bought property in the Caribbean and is going to try to raise dairy cows on a b ...