BotomSheetDialog FabButton锚视图无法正常工作 [英] BotomSheetDialog FabButton Anchor View not working as expected

查看:63
本文介绍了BotomSheetDialog FabButton锚视图无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Fab按钮锚定到BottomSheetDialog,但是它不起作用. 编辑 这是我的对话框

Hi I am trying to anchor Fab button to BottomSheetDialog but it is not working. EDITED Here is My Dialog

public static class QuickInfoDialog extends BottomSheetDialogFragment {

        private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {

            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                    dismiss();
                }

            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
            }
        };


        @Override
        public void onStart() {
            super.onStart();

            // Less dimmed background; see https://stackoverflow.com/q/13822842/56285
            Window window = getDialog().getWindow();
            WindowManager.LayoutParams params = window.getAttributes();
            params.dimAmount = 0.0f; // dim only a little bit
            window.setAttributes(params);

            // Transparent background; see https://stackoverflow.com/q/15007272/56285
            // (Needed to make dialog's alpha shadow look good)
            window.setBackgroundDrawableResource(android.R.color.transparent);
        }


        @Override
        public void setupDialog(Dialog dialog, int style) {
            super.setupDialog(dialog, style);
            View contentView = View.inflate(getContext(), R.layout.location_info_sheet, null);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

            dialog.setContentView(contentView);

            CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
            CoordinatorLayout.Behavior behavior = params.getBehavior();

            if (behavior != null && behavior instanceof BottomSheetBehavior) {
                ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
            }

//            FloatingActionButton navBtn = (FloatingActionButton) dialog.findViewById(R.id.btn_fab_direction);
//            FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) navBtn.getLayoutParams();
//            int boundary = (int) (layoutParams.topMargin + (layoutParams.width * 0.5));
//            layoutParams.topMargin = boundary;
//            LogUtils.LOGD(TAG, "boundary:  " + boundary);
//            navBtn.setLayoutParams(layoutParams);
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

        }
    }

这是我的xml,其中定义了其他布局和fab按钮.

This is my xml where my other layouts and fab button is defined.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/location_bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="196dp"
    android:background="@android:color/transparent"
    android:gravity="bottom"
    android:orientation="vertical"
    app:behavior_hideable="true"
    app:behavior_peekHeight="0dp"
    app:layout_behavior="@string/bottom_sheet_behavior"
    tools:ignore="MissingPrefix">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent">


        <FrameLayout
            android:id="@+id/clickable_sheet_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="48dp"
            android:clickable="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="@dimen/eight_dp"
                    android:layout_marginTop="@dimen/eight_dp"
                    android:layout_weight="0.3"
                    android:gravity="center">

                    <ImageView
                        android:id="@+id/image_view"
                        android:layout_width="@dimen/location_info_size"
                        android:layout_height="@dimen/location_info_size"
                        android:padding="@dimen/eight_dp"
                        android:src="@drawable/ic_launcher" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="@dimen/eight_dp"
                    android:layout_marginTop="@dimen/eight_dp"
                    android:layout_weight="0.7"
                    android:gravity="center"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/name_tv"
                        fontPath="@string/font_neue_pro_regular"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="@dimen/four_dp"
                        android:textColor="@color/semi_black"
                        android:textSize="@dimen/sixteen_sp" />

                    <TextView
                        android:id="@+id/street_tv"
                        fontPath="@string/font_neue_pro_light"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="@dimen/four_dp"
                        android:textSize="@dimen/fourteen_sp" />

                    <TextView
                        android:id="@+id/address_tv"
                        fontPath="@string/font_neue_pro_light"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="@dimen/four_dp"
                        android:textSize="@dimen/fourteen_sp" />

                    <TextView
                        android:id="@+id/distance_tv"
                        fontPath="@string/font_neue_pro_light"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_vertical|left"
                        android:padding="@dimen/four_dp"
                        android:textSize="@dimen/twelve_sp"
                        android:textColor="@color/semi_grey"/>
                </LinearLayout>
            </LinearLayout>
        </FrameLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/btn_fab_direction"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:src="@drawable/ic_directions_white"
            app:layout_anchor="@id/app_bar_layout"
            app:layout_anchorGravity="bottom|end" />

    </FrameLayout>
</LinearLayout>

我从这里尝试过解决方案 带有FloatButton的Android BottomSheet ,但它不起作用.

I have tried solution from here Android BottomSheet with FloatButton but it did not work.

我从中显示此BottomSheet的片段布局.

Fragment Layout from which i am showing this BottomSheet.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">



        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_gravity="bottom"
            android:background="@color/style_color_accent" />
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <FrameLayout
            android:id="@+id/map_main_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/sixteen_dp"
            android:focusable="false"
            android:focusableInTouchMode="false">

            <LinearLayout
                android:id="@+id/layout_search_filter"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/search_input_tv"
                    fontPath="@string/font_neue_pro_light"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.85"
                    android:background="@drawable/bg_shadow_layout"
                    android:elevation="@dimen/eight_dp"
                    android:gravity="center|left"
                    android:hint="@string/search_input_et_hint"
                    android:paddingLeft="@dimen/eight_dp"
                    android:textColor="@color/semi_black"
                    android:textColorHint="@color/semi_grey"
                    android:textSize="@dimen/fourteen_sp" />

                <ImageView
                    android:id="@+id/location_filter_selector_iv"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="@dimen/eight_dp"
                    android:layout_weight="0.15"
                    android:background="@drawable/bg_shadow_layout"
                    android:elevation="@dimen/eight_dp"
                    android:padding="@dimen/twelve_dp"
                    android:src="@drawable/ic_filter_grey" />

            </LinearLayout>

        </RelativeLayout>

    </FrameLayout>

    <include layout="@layout/location_info_sheet" />

</android.support.design.widget.CoordinatorLayout>

推荐答案

您应该将CoordinatorLayout用作FAB的直接父级,并将View用作FAB的锚点.

You should use CoordinatorLayout as the direct parent of FAB and the View which is used as anchor for the FAB.

然后,行app:layout_anchor="@id/app_bar_layout"表示此FAB锚定到ID为"app_bar_layout"的视图.但似乎您没有ID为"app_bar_layout"的任何视图.

Then the line app:layout_anchor="@id/app_bar_layout" means, this FAB is anchor to the View which has the id "app_bar_layout". But it seems you dont have any View with id "app_bar_layout.

这篇关于BotomSheetDialog FabButton锚视图无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆