gpt4 book ai didi

android - view在parent的最后一个位置,但是还是被挡住了

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:18:03 24 4
gpt4 key购买 nike

我想做什么

BottomSheetDialogFragment 中,我想膨胀一个始终停留在屏幕底部的 View ,无论如何 state (collapsed / expanded) BottomSheetBehavior 位于。

我做了什么

BottomSheetDialogFragment 的子类中,我从 XML 中膨胀一个 View 并将其添加为 CoordinatorLayout 的子类(这是 BottomSheetDialogFragment 的 parent 的 parent ):

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setupBottomBar(getView());
}

private void setupBottomBar (View rootView) {
CoordinatorLayout parentView = (CoordinatorLayout) ((FrameLayout)rootView.getParent()).getParent();
parentView.addView(LayoutInflater.from(getContext()).inflate(R.layout.item_selection_bar, parentView, false), -1);
}

代码运行没有错误。
而当我使用 Layout Inspector 查看 View 层级时, View 结构也是正确的:

Layout Inspector Screenshot

您还可以下载布局检查器结果 here ,并使用您自己的 Android Studio 打开它。

问题

但是,即使它作为 CoordinatorLayout 的最后一个子元素插入,它仍然被 BottomSheetDialogFragment 阻止。
当我慢慢向下滚动 BottomSheetDialogFragemnt(从折叠状态到隐藏状态)时,我终于可以看到我想要在 fragment 后面膨胀的 View 。

View blocked

为什么会这样?

答案

正如@GoodDev 正确指出的那样,这是因为 Root View (design_bottom_sheet) 已被 BottomSheetDialog 设置为 Z 平移。
这提供了一个重要信息:不仅 View 层次结构中的顺序将决定其可见性,而且还决定其 Z 平移。

最好的办法是获取design_bottom_sheet的Z值,设置为底部栏布局。

private void setupBottomBar (View rootView) {
CoordinatorLayout parentView = (CoordinatorLayout) (rootView.getParent().getParent());
View barView = LayoutInflater.from(getContext()).inflate(R.layout.item_selection_bar, parentView, false);
ViewCompat.setTranslationZ(barView, ViewCompat.getZ((View)rootView.getParent()));
parentView.addView(barView, -1);
}

最佳答案

编辑 2

好的,现在我看到你的要求了,试试这个:

private void setupBottomBar (View rootView) {
CoordinatorLayout parentView = (CoordinatorLayout) ((FrameLayout)rootView.getParent()).getParent();
View view = LayoutInflater.from(getContext()).inflate(R.layout.item_selection_bar, parentView, false);
// using TranslationZ to put the view on top of bottom sheet layout
view.setTranslationZ(100);
parentView.addView(view, -1);
}

编辑:

好的,我检查了你的布局并检查了BottomSheetDialogFragment源代码,找到了原因:

BottomSheetDialogFragment中使用BottomSheetDialog对话框,方法setContentViewBottomSheetDialog使用 wrapInBottomSheet将内容 View 放在 R.id.design_bottom_sheet 布局中。因此,您需要覆盖 BottomSheetDialogFragmentpublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 来解决您的问题。

或者,将您的 setupBottomBar 方法更改为:

private void setupBottomBar (View rootView) {
FrameLayout frame = (FrameLayout)rootView.getParent();
frame.addView(LayoutInflater.from(getContext()).inflate(R.layout.item_selection_bar, frame, false), -1);
}

并在您的 item_selection_bar 布局文件中,更改高度和 layout_gravity:

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content">

BottomSheetDialogFragment文档说:模态 Bottom Sheet 。这是 DialogFragment 的一个版本,它使用 BottomSheetDialog 而不是 float 对话框来显示 Bottom Sheet 。

所以BottomSheetDialogFragment是一个DialogDialog是一个 float View ,所以当BottomSheetDialogFragment 正在显示。

关于android - view在parent的最后一个位置,但是还是被挡住了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49025643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com