安卓—自定义 AlertDialog 的样式

时间:2023-01-29 08:00:04

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

效果图:

安卓—自定义 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主题

安卓—自定义 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对话框&lpar;加载提示框&rpar;

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

  2. 自定义AlertDialog的样式

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

  3. 自定义AlertDialog&lpar;仿微信&rpar;

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

  4. AlertDialog&period;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官方教程&lpar;11&rpar;自定义ActionBar的样式&lpar;含重要的样式属性表及练习示例&rpar;

    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. URLConnection类介绍

    URLConnection是一个功能强大的抽象类,它表示指向URL指定资源的活动连接. 与URL类相比,它与服务器的交互提供了更多的控制机制.尤其服务器是HTTP服务器,可以使用URLConnecti ...

  2. asp&period;net LINQ实现数据分页

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  3. 移动端a链接点击时取出背景色及边框

    a{blr:expression(this.onFocus=this.blur())} :focus{outline:0;} /*去掉a标签的虚线框,避免出现奇怪的选中区域*/*{-webkit-ta ...

  4. 从四大音乐APP首页设计对比分析产品方向

    原帖:http://www.ui.cn/detail/63201.html 本文章中作者例举四个音乐APP应用:虾米.网易.百度.QQ首页 1. 推荐内容:作者将四个首页界面划分出官方推荐与个性化推荐 ...

  5. android 播放assets文件里视频文件的问题

    今天做了一个功能,就是播放项目工程里面的视频文件,不是播放SD卡视频文件. 因为之前写webview加载assets文件夹时,是这样写的: webView = new WebView(this); w ...

  6. Codeforce&num;354&lowbar;B&lowbar;Pyramid of Glasses&lpar;模拟&rpar;

    题目连接:http://codeforces.com/contest/676/problem/B 题意:给你一个N层的杯子堆成的金字塔,倒k个杯子的酒,问倒完后有多少个杯子的酒是满的 题解:由于数据不 ...

  7. JavaScript 特效三大系列总结

    一. offset系列 1. offset系列的5个属性 1. offsetLeft : 用于获取元素到最近的定位父盒子的左侧距离 * 计算方式: 当前元素的左边框的左侧到定位父盒子的左边框右侧 * ...

  8. Matlab生成&period;exe可执行程序

    由于在教学过程中需要演示Matlab程序,而教学机又未安装Matlab程序,因此有必要将Matlab程序生成.exe可执行程序,便于直接执行. 在Matlab中提供了Complier,可直接使用. ( ...

  9. &num;if和&num;ifdef的区别

    学习STM32偶然发现:在Keil中直接预先定义宏USE_STDPERIPH_DRIVER,但是却没有指定宏的值.而在头文件中判断用的是如下代码: #ifdef USE_STDPERIPH_DRIVE ...

  10. 利用captcha库绘制验证码

    #导包 from captcha.image import ImageCaptcha from PIL import Image import random import time import os ...