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

pul*_*ion 7 android android-layout android-dialogfragment

我想显示一个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>
Run Code Online (Sandbox Code Playgroud)

这是应该在其上旋转View的背景ProgressBar

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

在此处输入图片说明

这是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();
        }

    }
Run Code Online (Sandbox Code Playgroud)

为什么它没有按预期显示我的自定义布局?

编辑:作为附加说明,我的问题是背景View应该占据屏幕的 60% 区域(高度方面),为此我将其限制在指南之间。这是一些东西,我无法通过其他关于DialogFragment. 此外,我想知道,为什么 aDialogFragment通常默认情况下不显示正确的?

小智 20

如果把 ConstraintLayout 改成Relative或者Linear,这个问题就解决了。

  • 这是什么原因?我面临同样的问题。我有两个不同的源代码。当我切换到新的源代码时,它停止工作。:( 我在其他屏幕上尝试了相同的 ConstraintLayout 并且它可以正常工作,但是对于 DialogFragment,它在我的新源代码中不起作用。 (2认同)

Aan*_*hta 6

我遇到了同样的问题,我将 ConstraintLayout 放在 RelativeLayout 中并且它起作用了。

我对 LinearLayout 尝试了同样的方法,但没有奏效。


Nil*_*hod 1

尝试这个

 Window window = yourDialog.getWindow();
 window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
 window.setGravity(Gravity.CENTER);
Run Code Online (Sandbox Code Playgroud)

或者

int width = getResources().getDimensionPixelSize(R.dimen.popup_width);
int height =  getResources().getDimensionPixelSize(R.dimen.popup_height);        
getDialog().getWindow().setLayout(width, height);
Run Code Online (Sandbox Code Playgroud)