美文网首页
android自定义dialog记录

android自定义dialog记录

作者: 层林尽染lr | 来源:发表于2019-04-29 17:19 被阅读0次

自定义dialog时两个问题记录一下

一.自定义的dialog里面有列表的时候,并且要限制最大宽高
此时处理两点:
1.添加一下代码至自定义的dialog中,动态计算宽高

    public void onWindowFocusChanged(boolean hasFocus) {
        if (!hasFocus) {
            return;
        }
        setHeight();
    }

/**
 * 设置弹框的大小
 */
private void setHeight() {
    Window window = getWindow();
    DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
    WindowManager.LayoutParams attributes = window.getAttributes();
    if (window.getDecorView().getHeight() >= (int) (displayMetrics.heightPixels * 0.8)) {
        attributes.height = (int) (displayMetrics.heightPixels * 0.8);
    }

    if (window.getDecorView().getWidth() >= (int) (displayMetrics.widthPixels * 0.8)) {
        attributes.width = (int) (displayMetrics.widthPixels * 0.8);
    }
    window.setAttributes(attributes);
}

2.给dialog布局中的recycleView设置权重

二.dialog布局中的textView宽要设置成match_parent,否则可能会显示不全

相关文章

网友评论

      本文标题:android自定义dialog记录

      本文链接:https://www.haomeiwen.com/subject/rtkunqtx.html