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

效果图:

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. 列表 元祖 range

    1.列表 list 存放一些数据的容器 比如 衣柜 书包 作用:存储一些数据,数据量比较大 可以下标 可以切片 可以步长 和字符串的完全一样 lst = [1,2,3] print(lst) #[1, ...

  2. 从零开始入门 K8s | 应用存储和持久化数据卷:核心知识

    作者 | 至天 阿里巴巴高级研发工程师 一.Volumes 介绍 Pod Volumes 首先来看一下 Pod Volumes 的使用场景: 场景一:如果 pod 中的某一个容器在运行时异常退出,被 ...

  3. 关于CSS书写规范、顺序

    关于CSS的书写规范和顺序,是大部分前端er都必须要攻克的一门关卡,如果没有按照良好的CSS书写规范来写CSS代码,会影响代码的阅读体验.这里总结了一个CSS书写规范.CSS书写顺序供大家参考,这些是 ...

  4. shell检测网站地址是否存活

    #!/bin/bash . /etc/init.d/functions url_list=(www.baidu.com) ChkCurl(){ i=0 while [ $i -lt 2 ] do cu ...

  5. 三个Eclipse下的Debug的使用场景(五)

    本文链接:https://blog.csdn.net/u011781521/article/details/55000066    http://blog.csdn.net/u010075335/ar ...

  6. 【Java】后台将文件上传至远程服务器

    问题:由于系统在局域网(能访问外网)内,但外网无法请求局域网内服务器文件和进行处理文件. 解决:建立文件服务器,用于存储文件及外网调用. 客户端(文件上传): package cn.hkwl.lm.u ...

  7. php微信支付v3版本签名生成

    前几天需要对接微信支付卡包营销活动需要对接微信新版SDKv3版 签名生成规则,微信的官方文档里面说明的还算可以吧,不过个人觉得不太理想- -.  自己调试的时候调试了半天才找了错误原因. https: ...

  8. JVM垃圾回收算法详解

    前言 在JVM内存模型中会将堆内存划分新生代.老年代两个区域,两块区域的主要区别在于新生代存放存活时间较短的对象,老年代存放存活时间较久的对象,除了存活时间不同外,还有垃圾回收策略的不同,在JVM中中 ...

  9. python学习(索引/切片)

    一.索引 1.索引值从左到右-->从0开始,索引值从右到左-->从-1开始 取值格式var[index] >>> name = "xinfangshuo&quo ...

  10. Java8两大特性(一)——Stream

    什么是Stream? Stream(流)是一个来自数据源的元素队列并且支持聚合操作,元素流在管道中经过中间操作,最终操作得到结果. 数据源:集合,数组,I/O channel,产生器generator ...