【问题标题】:Dialog not respecting MATCH_PARENT对话框不尊重 MATCH_PARENT
【发布时间】:2018-02-17 10:13:49
【问题描述】:

我打算用自定义布局显示Dialog

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
      android:background="#ffffff"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="@dimen/lef_margin"
        android:layout_marginTop="@dimen/lef_margin"
        android:background="#ffffff"
        android:fitsSystemWindows="true" />

    <ImageView
        android:id="@+id/imgsocialauthclose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_gravity="right|top"
        android:background="@drawable/closexml" />

</FrameLayout>

现在,如果我通过Dialog.setContentView() 将此布局设置为对话框,我会得到如下所示的内容(请注意,相关对话框是中间带有十字按钮的白色框,而不是一个在后台包含所有社交媒体选项):

现在,如果我要在 setContentView() 调用之外编写这一附加行,我会得到一个扩展到覆盖父视图的对话框:

dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

如果您观察布局,您会注意到 MATCH_PARENT 约束已被声明。那么,为什么我需要做这个额外的行,或者,

为什么对话框默认不正确遵守 MATCH_PARENT 约束?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    我通过将其添加到onCreateDialog 解决了一次对话片段的问题。这将使您的对话框全屏显示。然后,您可以使用透明背景定义边距/填充,以您想要的任何方式塑造您的对话框。

    Dialog dialog = new Dialog(getActivity() ,android.R.style.Theme_Holo_NoActionBar);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    

    然后显示对话框只需使用

    MyDialogFragment fragment = new MyDialogFragment();
    fragment.show(getChildFragmentManager(), "addPlaylistDialog");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多