微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我想做的事

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

我做了什么

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

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

Layout Inspector Screenshot

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

问题

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

View blocked

为什么会这样?

答案

正如@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内容.

原文地址:https://www.jb51.cc/android/1075125.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐