Android自定义弹出框dialog效果

发布时间 - 2026-01-11 02:00:43    点击率:

项目要用到弹出框,还要和苹果的样式一样(Android真是没地位),所以就自己定义了一个,不是很像(主要是没图),但是也还可以。

废话不多说了,直接上代码

1、先看布局文件

<?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:padding="20dp"
 android:orientation="vertical">

 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center_horizontal"
  android:background="@drawable/custom_dialog_background"
  android:orientation="vertical">

  <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center_horizontal"
   android:orientation="vertical">

   <TextView
    android:id="@+id/tv_title_custom_dialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="提醒"
    android:textColor="#000"
    android:textSize="18dp" />

   <TextView
    android:id="@+id/tv_message_custom_dialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="您确定要取消订单吗" />
  </LinearLayout>

  <View
   android:layout_width="match_parent"
   android:layout_height="0.5dp"
   android:layout_marginTop="20dp"
   android:background="#dfdfdf" />

  <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal">

   <Button
    android:id="@+id/btn_negative_custom_dialog"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:color/transparent"
    android:text="取消"
    android:textColor="@android:color/holo_blue_dark" />

   <View
    android:layout_width="0.5dp"
    android:layout_height="match_parent"
    android:background="#dfdfdf" />

   <Button
    android:id="@+id/btn_positive_custom_dialog"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:color/transparent"
    android:text="确定"
    android:textColor="@android:color/holo_blue_dark" />
  </LinearLayout>
 </LinearLayout>
</LinearLayout>

2、集成dialog重写了一下

package newair.com.storelibrary.ui.custom.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import newair.com.storelibrary.R;

/**
 * Created by ouhimehime on 16/4/22.
 * ---------自定义提示框-----------
 */
public class CustomDialog extends Dialog {


 public CustomDialog(Context context) {
  super(context);
 }

 public CustomDialog(Context context, int themeResId) {
  super(context, themeResId);
 }

 protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
  super(context, cancelable, cancelListener);
 }

 public static class Builder {
  private Context context;
  private String title; //标题
  private String message;//提示消息
  private String negative_text;//消极的
  private String positive_text;//积极的
  private DialogInterface.OnClickListener negativeListener;//消极的监听
  private DialogInterface.OnClickListener positiveListener;//积极的监听

  public Builder(Context context) {
   this.context = context;
  }

  public Builder setTitle(String title) {
   if (title == null) {
    this.title = "提醒";
   }
   this.title = title;
   return this;
  }

  public Builder setMessage(String message) {
   if (message == null) {
    this.message = "您没有填写提示信息哦";
   }
   this.message = message;
   return this;
  }

  public Builder setNegativeButton(String negative_text, DialogInterface.OnClickListener negativeListener) {
   if (negative_text == null) {
    this.negative_text = "取消";
   }
   this.negative_text = negative_text;
   this.negativeListener = negativeListener;

   return this;
  }

  public Builder setPositionButton(String positive_text, DialogInterface.OnClickListener positiveListener) {
   if (positive_text == null) {
    this.positive_text = "确定";
   }
   this.positive_text = positive_text;
   this.positiveListener = positiveListener;

   return this;
  }

  private TextView tv_title_custom_dialog; //标题
  private TextView tv_message_custom_dialog;//提示信息
  private Button btn_negative_custom_dialog;//消极
  private Button btn_positive_custom_dialog;//积极


  public CustomDialog create() {
   final CustomDialog dialog = new CustomDialog(context);
   View view = LayoutInflater.from(context).inflate(R.layout.dialog_custom_style_layout, null);
   dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//加上这一句,取消原来的标题栏,没加这句之前,发现在三星的手机上会有一条蓝色的线
//   dialog.addContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
   dialog.setContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
   tv_title_custom_dialog = (TextView) view.findViewById(R.id.tv_title_custom_dialog);
   tv_message_custom_dialog = (TextView) view.findViewById(R.id.tv_message_custom_dialog);
   btn_negative_custom_dialog = (Button) view.findViewById(R.id.btn_negative_custom_dialog);
   btn_positive_custom_dialog = (Button) view.findViewById(R.id.btn_positive_custom_dialog);
   tv_title_custom_dialog.setText(title);
   tv_message_custom_dialog.setText(message);
   btn_negative_custom_dialog.setText(negative_text);
   btn_positive_custom_dialog.setText(positive_text);
   dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
   btn_negative_custom_dialog.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
     negativeListener.onClick(dialog, Dialog.BUTTON_NEGATIVE);
    }
   });
   btn_positive_custom_dialog.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
     positiveListener.onClick(dialog, Dialog.BUTTON_POSITIVE);
    }
   });
   return dialog;
  }
 }
}

3、使用起来和系统的用法一样

CustomDialog.Builder builder = new CustomDialog.Builder(this);
    builder.setTitle("购物提醒")
      .setMessage("我是提示信息,大家好好")
      .setNegativeButton("再看看", new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
        Toast.makeText(GoodsListActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
       }
      })
      .setPositionButton("确定", new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
        Toast.makeText(GoodsListActivity.this, "点击了确定按钮", Toast.LENGTH_SHORT).show();
       }
      })
      .create()
      .show();

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# Android  # 弹出框  # dialog  # Android自定义底部弹出框ButtomDialog  # Android仿微信网络加载弹出框  # Android自定义view仿iOS弹出框效果  # Android使用popUpWindow带遮罩层的弹出框  # Android实现底部半透明弹出框PopUpWindow效果  # Android实现蒙版弹出框效果  # Android 多种简单的弹出框样式设置代码  # Android实现可输入数据的弹出框  # Android使用Dialog风格弹出框的Activity  # android自定义弹出框样式的实现方法  # 提示信息  # 我是  # 这一  # 会有  # 说了  # 不多  # 要用  # 弹出  # 写了  # 自定义  # 很像  # 这句  # 再看看  # 先看  # 也还  # 大家多多  # 主要是  # 标题栏  # 您没有  # textColor 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 文字头像制作网站推荐软件,醒图能自动配文字吗?  Laravel如何配置和使用队列处理异步任务_Laravel队列驱动与任务分发实例  Laravel如何配置Horizon来管理队列?(安装和使用)  如何选择PHP开源工具快速搭建网站?  lovemo网页版地址 lovemo官网手机登录  如何做网站制作流程,*游戏网站怎么搭建?  如何实现javascript表单验证_正则表达式有哪些实用技巧  如何在沈阳梯子盘古建站优化SEO排名与功能模块?  百度输入法全感官ai怎么关 百度输入法全感官皮肤关闭  Laravel如何生成和使用数据填充?(Seeder和Factory示例)  使用spring连接及操作mongodb3.0实例  怎么用AI帮你设计一套个性化的手机App图标?  如何用ChatGPT准备面试 模拟面试问答与职场话术练习教程  IOS倒计时设置UIButton标题title的抖动问题  微信小程序 require机制详解及实例代码  Laravel如何实现RSS订阅源功能_Laravel动态生成网站XML格式订阅内容【教程】  Laravel数据库迁移怎么用_Laravel Migration管理数据库结构的正确姿势  Laravel Livewire是什么_使用Laravel Livewire构建动态前端界面  laravel怎么为API路由添加签名中间件保护_laravel API路由签名中间件保护方法  Laravel如何处理跨站请求伪造(CSRF)保护_Laravel表单安全机制与令牌校验  家族网站制作贴纸教程视频,用豆子做粘帖画怎么制作?  Laravel Telescope怎么调试_使用Laravel Telescope进行应用监控与调试  Laravel如何实现邮箱地址验证功能_Laravel邮件验证流程与配置  Laravel如何处理文件上传_Laravel Storage门面实现文件存储与管理  Laravel观察者模式如何使用_Laravel Model Observer配置  微信小程序 scroll-view组件实现列表页实例代码  Linux安全能力提升路径_长期防护思维说明【指导】  如何正确下载安装西数主机建站助手?  如何为不同团队 ID 动态生成多个“认领值班”按钮  企业网站制作这些问题要关注  Linux后台任务运行方法_nohup与&使用技巧【技巧】  CSS3怎么给轮播图加过渡动画_transition加transform实现【技巧】  如何快速搭建个人网站并优化SEO?  矢量图网站制作软件,用千图网的一张矢量图做公司app首页,该网站并未说明版权等问题,这样做算不算侵权?应该如何解决?  Edge浏览器怎么启用睡眠标签页_节省电脑内存占用优化技巧  标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?  标题:Vue + Vuex 项目中正确使用 JWT 进行身份认证的实践指南  在centOS 7安装mysql 5.7的详细教程  nodejs redis 发布订阅机制封装实现方法及实例代码  如何在阿里云完成域名注册与建站?  ,交易猫的商品怎么发布到网站上去?  Laravel怎么在Blade中安全地输出原始HTML内容  Laravel怎么进行数据库回滚_Laravel Migration数据库版本控制与回滚操作  韩国网站服务器搭建指南:VPS选购、域名解析与DNS配置推荐  Laravel怎么集成Log日志记录_Laravel单文件与每日日志配置及自定义通道【详解】  Laravel的Blade指令怎么自定义_创建你自己的Laravel Blade Directives  香港服务器选型指南:免备案配置与高效建站方案解析  三星、SK海力士获美批准:可向中国出口芯片制造设备  如何快速搭建自助建站会员专属系统?  Laravel的契約(Contracts)是什么_深入理解Laravel Contracts与依赖倒置