zoukankan      html  css  js  c++  java
  • android中的Dialog

    一、Dialog概述

    二、使用系统自带的Dialog

    1、新建Builder

        AlertDialog.Builder builder = new AlertDialog.Builder(StoryActivity.this);
        dialog.show();
    

    2、通过builder创建dialog

        AlertDialog dialog = builder.setView(view).setTitle("查看评论").setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }    
        }).create();
    

    3、关于NegativeButton、PositiveButton、NeutralButton

    早期版本,NegativeButton最右侧、PositiveButton最左侧、NeutralButton中间

    在android4.0中,NegativeButton在最左侧、PositiveButton在最右侧

    二、如何自定义Dialog

    1、特别注意!!!(首先明确)

    1.1 setContentView、setView与show()方法相对位置引起异常

    如使用setContentView、setView方法来自定义View的话,须与show()方法保持先后顺序

        dialog.show();
        dialog.setContentView(contentView);
    

    2.2 自定义Dilog之后,setTitle等方法无效

    须自行在界面中添加

    2、步骤同上

    3、设置界面

    使用以下方法实现自定义界面

        setView(View view)
        setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight,int viewSpacingBottom)
        setContentView(@LayoutRes int layoutResID)
        setContentView(View view)
        setContentView(View view, ViewGroup.LayoutParams params)//params Layout parameters for the view.
    

    4、自定义Dialog的宽高

        Window window = this.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.height = dialogHeight;//设置你想要的宽高
        window.setAttributes(params);
    

    5、获得屏幕的宽高

        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高
        d.heightPixels//取得px单位的值
        d.xdpi//取得dp单位的值
    
  • 相关阅读:
    deepin 安装版本管理工具
    deepin 安装maven
    deepin 安装 idea
    启动VMware环境下的Linux操作系统,添加新分区
    Centos6.*安装vsftpd
    easyui-datebox 年月视图显示
    oracle-数据库泵EXPDP导出用户下所有
    Oracle虚拟机配置
    JSON理解
    Python语法基础_04.元组、函数-上
  • 原文地址:https://www.cnblogs.com/neillee/p/5421646.html
Copyright © 2011-2022 走看看