Dialog(五)——自定义对话框(Dialog)
发布日期:2021-06-30 11:15:19
浏览次数:2
分类:技术文章
本文共 4685 字,大约阅读时间需要 15 分钟。
MainActivity如下:
package cn.testalertdialog;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.app.Dialog;import android.content.Context;import android.os.Bundle;import android.util.DisplayMetrics;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.view.WindowManager;import android.widget.Button;/** * Demo说明: * * 自定义Dialog的使用 * * 1 在屏幕中显示自定义Dialog. * 其中按照屏幕大小设置Dialog的显示大小 * * 2 在屏幕底部显示自定义Dialog. * 网上有许多资料都有一个缺陷:Dialog距离屏幕底部总有几毫米的间隙. * 试过许多方法和style都不管用.在此给出一个可行的办法.详见代码. * 3 本示例采用API 15即 4.0.3 * * http://blog.csdn.net/lfdfhl * */public class MainActivity extends Activity { private Button mButton; private Dialog mDialog; private AlertDialog mAlertDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mButton=(Button) findViewById(R.id.button); mButton.setOnClickListener(new ButtonOnClickListenerImpl()); } private class ButtonOnClickListenerImpl implements OnClickListener { @Override public void onClick(View view) { switch (view.getId()) { case R.id.button: showDialog(MainActivity.this); //showDialogAtScreenBottom(MainActivity.this); break; default: break; } } } //显示自定义Dialog private void showDialog(Context context) { LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View dialogView = inflater.inflate(R.layout.dialogview, null); mDialog = new Dialog(context, R.style.dialog); mDialog.setContentView(dialogView); Window dialogWindow = mDialog.getWindow(); // 获取对话框当前的参数值 WindowManager.LayoutParams layoutParams = dialogWindow.getAttributes(); // 设置对话框高度为屏幕高度的0.3倍 layoutParams.height = (int) (getScreenHeight(context) * 0.3); // 设置对话框宽度为屏幕宽度的0.7倍 layoutParams.width = (int) (getScreenWidth(context) * 0.7); dialogWindow.setAttributes(layoutParams); mDialog.show(); initDialog(dialogView); } //在屏幕底部显示自定义Dialog private void showDialogAtScreenBottom(Context context) { LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View dialogView = inflater.inflate(R.layout.dialogview, null); mAlertDialog = new AlertDialog.Builder(context, R.style.dialog).create(); mAlertDialog.show(); Window window = mAlertDialog.getWindow(); // 将Dialog显示到屏幕的底部 window.setGravity(Gravity.BOTTOM); // 去掉Dialog本身的padding window.getDecorView().setPadding(0, 0, 0, 0); WindowManager.LayoutParams layoutParams = mAlertDialog.getWindow().getAttributes(); // 设置宽度为屏幕宽度 layoutParams.width = getScreenWidth(context); mAlertDialog.getWindow().setAttributes(layoutParams); mAlertDialog.setContentView(dialogView); initAlertDialog(dialogView); } //初始化自定义布局中的控件 private void initDialog(View dialogView){ Button firstButton=(Button) dialogView.findViewById(R.id.dialog_firstButton); firstButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { System.out.println("点击确定"); } }); Button secondButton=(Button) dialogView.findViewById(R.id.dialog_secondButton); secondButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mDialog!=null&&mDialog.isShowing()) { mDialog.dismiss(); } } }); } //初始化自定义布局中的控件 private void initAlertDialog(View dialogView) { Button firstButton = (Button) dialogView.findViewById(R.id.dialog_firstButton); firstButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { System.out.println("点击确定"); } }); Button secondButton = (Button) dialogView.findViewById(R.id.dialog_secondButton); secondButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mAlertDialog != null && mAlertDialog.isShowing()) { mAlertDialog.dismiss(); } } }); } /** * 获得屏幕高度 */ public static int getScreenWidth(Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.widthPixels; } /** * 获得屏幕宽度 */ public static int getScreenHeight(Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.heightPixels; }}
main.xml如下:
dialogview.xml如下:
style如下:
转载地址:https://it9527.blog.csdn.net/article/details/28891003 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月17日 22时46分36秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
ELK日志分析系统原理与部署
2019-04-30
Nginx访问状态以及基于多域名、多端口、多IP配置虚拟主机
2019-04-30
Nginx优化及防盗链
2019-04-30
Docker简介和安装优化
2019-04-30
Docker之镜像、容器的管理命令详解
2019-04-30
Docker-构建镜像、私有仓库registry、数据卷、端口映射、容器互联
2019-04-30
Tomcat安装部署、构建虚拟主机以及优化参数
2019-04-30
Nginx 之 Rewrite和具体场景
2019-04-30
Dockerfile构建编译MYSQL-5.6、MYSQL-5.7镜像
2019-04-30
Docker Compose容器编排工具
2019-04-30
docker的资源控制(CPU、内存、IO)
2019-04-30
Docker Consul 工具(理论+实操)
2019-04-30
Docker容器通信安全----TLS加密通讯
2019-04-30
Docker-------私有仓库 Harbor 的搭建
2019-04-30
搭建KVM虚拟化平台(实战+理论)
2019-04-30
OpenStack入门及原理基础理论知识点
2019-04-30
openstack的keystone认证服务
2019-04-30
OpenStack架构搭建实战(命令可复制)
2019-04-30
openstack的glance镜像服务
2019-04-30