我想显示一个没有标题、按钮等的 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>
这里,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();
}
}
为什么它没有按预期显示我的自定义布局?
编辑: 作为附加说明,我的问题是背景 View
应该占据屏幕的 60% 区域(高度方面)并且对于我已将其限制在指南之间。这是我无法通过对有关 DialogFragment
的类似问题的其他答案来实现的。此外,我想知道,为什么 DialogFragment
通常默认不显示正确的内容?
最佳答案
如果将 ConstraintLayout 更改为 Relative 或 Linear 这个问题就迎刃而解了。
关于android - 为什么我的 DialogFragment 没有显示自定义布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44942440/