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

标签 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>

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

    }

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

编辑: 作为附加说明,我的问题是背景 View 应该占据屏幕的 60% 区域(高度方面)并且对于我已将其限制在指南之间。这是我无法通过对有关 DialogFragment 的类似问题的其他答案来实现的。此外,我想知道,为什么 DialogFragment 通常默认不显示正确的内容?

最佳答案

如果将 ConstraintLayout 更改为 Relative 或 Linear 这个问题就迎刃而解了。

关于android - 为什么我的 DialogFragment 没有显示自定义布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44942440/

相关文章:

android - groovy 是一种潜在的 Android 开发语言吗

android - 如何在保留 Android 4.0 向后兼容选项菜单的同时启用硬件加速?

java - java字符串中的立方根符号

android - 向 RadioGroup 和 Radiobutton 添加过渡动画

java - Android:IllegalStateException 调用 DialogFragment

android - 使用 DialogFragment 的对话框未正确显示

java - 公共(public)方法对同一包中的其他类不可见

android - android 中的 webview.loadUrl() 问题?

java - ArrayList<HashMap<String, String>> 到 SQLite 数据库 android

android-layout - 如何将 Android XML 拆分为三个相等的布局矩形?