android–View位于父级的最后位置,但仍被阻止

android–View位于父级的最后位置,但仍被阻止,第1张

概述我想做的事在BottomSheetDialogFragment中,我想要一个始终坚持在屏幕底部的视图,无论BottomSheetBehavior在state(collapsed/expanded)中.我做了什么在BottomSheetDialogFragment的子类中,我从XML中扩展了一个视图并将其添加为CoordinatorLayout的子节点(这是BottomSheetDial

我想做的事

在BottomSheetDialogFragment中,我想要一个始终坚持在屏幕底部的视图,无论BottomSheetBehavior在state (collapsed / expanded)中.

我做了什么

在BottomSheetDialogFragment的子类中,我从XML中扩展了一个视图并将其添加为CoordinatorLayout的子节点(这是BottomSheetDialogFragment的父节点的父节点):

@OverrIDepublic 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层次结构时,视图结构也是正确的:

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

问题

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

为什么会这样?

答案

正如@GoodDev正确指出的那样,这是因为根视图(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对话框,方法setContentVIEw在BottomSheetDialog中使用wrapInBottomSheet将内容视图放在R.ID.design_bottom_sheet布局中.因此,您需要覆盖BottomSheetDialogFragment的公共视图onCreateVIEw(LayoutInflater inflater,VIEwGroup容器,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 doc说:模态底页.这是DialogFragment的一个版本,它使用BottomSheetDialog而不是浮动对话框显示底部工作表.

因此BottomSheetDialogFragment是一个Dialog,Dialog是一个浮动视图,因此当BottomSheetDialogFragment显示时将覆盖Activity内容.

总结

以上是内存溢出为你收集整理的android – View位于父级的最后位置,但仍被阻止全部内容,希望文章能够帮你解决android – View位于父级的最后位置,但仍被阻止所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1119623.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存