当前位置: 移动技术网 > 科技>操作系统>windows > dialog四周留白以及圆角样式设置无效的问题

dialog四周留白以及圆角样式设置无效的问题

2020年08月14日  | 移动技术网科技  | 我要评论
8.11的关于dialog的两个问题1关于Dialog无法全屏的原因自定义的dialog如果不指定样式的话,会自动继承系统默认的样式,在四周留下空隙。项目中最初自定义dialog的构造函数如下。public MyDialog(@NonNull Context context) { super(context);}查看源码点进去以后会发现如果不指定样式,会使用这个默认的系统样式。/** * Creates a dialog window that uses the default di

8.11的关于dialog的两个问题

1关于Dialog无法全屏的原因

自定义的dialog如果不指定样式的话,会自动继承系统默认的样式,在四周留下空隙。项目中最初自定义dialog的构造函数如下。

public MyDialog(@NonNull Context context) {
    super(context);
}

查看源码点进去以后会发现如果不指定样式,会使用这个默认的系统样式。

/**
 * Creates a dialog window that uses the default dialog theme.
 * <p>
 * The supplied {@code context} is used to obtain the window manager and
 * base theme used to present the dialog.
 *
 * @param context the context in which the dialog should run
 * @see android.R.styleable#Theme_dialogTheme
 */
public Dialog(@NonNull Context context) {
    this(context, 0, true);
}

而系统样式里面有很多属性这里都省略了,直接看和本次有关的属性,可以看到设置了上下左右的内边距都为10dp,因此是无法显示全屏的。

<style name="Theme.Dialog">
    ……
 
    <item name="listPreferredItemPaddingLeft">10dip</item>
    <item name="listPreferredItemPaddingRight">10dip</item>
    <item name="listPreferredItemPaddingStart">10dip</item>
    <item name="listPreferredItemPaddingEnd">10dip</item>

    <item name="preferencePanelStyle">@style/PreferencePanel.Dialog</item>
</style>

修改方式也很简单

@Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_classcourse_dialogdiscount);
        Window window = getWindow();
        if (null != window) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.gravity = Gravity.BOTTOM;
            window.getDecorView().setPadding(0,0,0,0);

            window.setAttributes(params);
        }
    }

2设置圆角背景没用

dialog默认的样式是有背景的,需要设置为透明

<item name="android:background">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>

或者可以在代码中将背景设置为透明

 window.getDecorView().setBackgroundResource(R.color.transparent);

本文地址:https://blog.csdn.net/mrkyee/article/details/107964739

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网