为什么我的DialogFragment没有显示自定义布局?

8

我想展示一个没有标题、按钮等(即典型对话框中包含的所有内容)的DialogFragment,只有一个放置在纯色背景上的ProgressBar旋转器。大致上来说,背景的宽度与父视图相同,旋转器位于其中心位置。为此,我有一个自定义布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <View
        android:id="@+id/search_progress_alpha_indicator"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline2"
        android:background="#00ffffff"/>

    <ProgressBar
        android:id="@+id/search_tutors_progress_bar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:visibility="gone"/>

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline2"
        app:layout_constraintGuide_percent="0.20"
        android:orientation="horizontal"
        tools:layout_editor_absoluteY="126dp"
        tools:layout_editor_absoluteX="0dp" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline3"
        app:layout_constraintGuide_percent="0.80"
        android:orientation="horizontal"
        tools:layout_editor_absoluteY="378dp"
        tools:layout_editor_absoluteX="0dp" />


</android.support.constraint.ConstraintLayout>

在这里,ViewProgressBar 应该旋转的背景。

但是当我在 DialogFragment 中填充这个布局时,它显示了完全不同的东西:

enter image description here

下面是 DialogFragment 代码:

public static class SearchIndicatorDialogFragment extends DialogFragment{

        public static String FRAGMENT_TAG = "searchIndDiaFragTag";

        private View alphaSearchIndicator;
        private ProgressBar progressBar;


        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

            View dialogRootView = inflater.inflate(R.layout.fragment_search_dialog, container, false);

            // Obtain the ProgressBar
            progressBar = (ProgressBar) dialogRootView.findViewById(R.id.search_tutors_progress_bar);

            // Obtain the Alpha search indicator
            alphaSearchIndicator = dialogRootView.findViewById(R.id.search_progress_alpha_indicator);

            return dialogRootView;
        }

        @Override
        public void onStop() {

            progressBar.setVisibility(View.GONE);

            alphaSearchIndicator.setBackgroundColor(0x00ffffff);

            super.onStop();
        }

        @Override
        public void onResume() {
            progressBar.setVisibility(View.VISIBLE);

            alphaSearchIndicator.setBackgroundColor(0xA6ffffff);

            super.onResume();
        }

    }

为什么我的自定义布局没有按照预期显示?
编辑:另外需要注意的是,在我的情况下,背景视图应占屏幕高度的60%,因此我已将其限制在指南线之间。这是其他类似关于DialogFragment的问题的答案无法实现的。此外,我想知道为什么DialogFragment通常不能默认正确显示?

https://dev59.com/7WAg5IYBdhLWcg3wFHqF - IntelliJ Amiya
@IntelliJAmiya:看到了。在我的情况下,背景视图应该占据屏幕高度的50%区域,为此我已将其约束在指南之间。那个链接的答案可以轻松地将match_parent宽度与LayoutParams匹配,但是对于像我这样需要处理复杂布局的情况,我该怎么办呢? - Manish Kumar Sharma
6个回答

24

如果您将ConstraintLayout更改为Relative或Linear 这个问题将得到解决。


哇,我不明白这是怎么工作的。我正在使用AppCompatDialogFragment。 - Rishabh876
2
这是什么原因呢?我面临同样的问题。我有两个不同的源代码。当我切换到新的源代码时,它停止工作了。:( 我尝试了使用其他屏幕相同的ConstraintLayout,它可以正常工作,但在DialogFragment中,它在我的新源代码中无法工作。 - Aanal Shah

8

我曾经遇到过同样的问题,我将我的ConstraintLayout放到RelativeLayout中,这样做起作用了。

我也尝试了使用LinearLayout,但是它不起作用。


3
把ConstraintLayout嵌套在RelativeLayout中,问题就解决了。

2

试试这个:

    AlertDialog builder = new AlertDialog.Builder(this).create();
    builder.setView(progressbar_id);
    Window window = builder.getWindow();
    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    builder.show();

1
尝试这个。
 Window window = yourDialog.getWindow();
 window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
 window.setGravity(Gravity.CENTER);

or

int width = getResources().getDimensionPixelSize(R.dimen.popup_width);
int height =  getResources().getDimensionPixelSize(R.dimen.popup_height);        
getDialog().getWindow().setLayout(width, height);

@pulp_fiction 在 onResume() 中调用,而不是在 onCreateView() 中。 - AskNilesh
好的,这次我在onResume()中完成了它,但是显示不正确。颜色不是我想要的,高度也不是我想要的。 - Manish Kumar Sharma

0

尝试更改

View dialogRootView = inflater.inflate(R.layout.fragment_search_dialog, container, false);

 View dialogRootView = inflater.inflate(R.layout.fragment_search_dialog, container, true);

我想我们应该负责将布局文件的View附加到其根ViewGroup。


不,无论是否将其附加为true,输出都完全相同。 - Manish Kumar Sharma
刚刚做完了,没有改变 :( - Manish Kumar Sharma
请还要添加如何将对话框片段附加到rootViewGroup的代码 - Jay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接