【问题标题】:BotomSheetDialog FabButton Anchor View not working as expectedBotomSheetDialog FabButton 锚视图未按预期工作
【发布时间】:2016-08-27 09:26:03
【问题描述】:

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

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 按钮。

<?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>

我已经尝试过这里的解决方案 Android BottomSheet with FloatButton 但它不起作用。

我从中显示此 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>

【问题讨论】:

  • 请添加截图

标签: android android-support-library androiddesignsupport


【解决方案1】:

您应该使用 CoordinatorLayout 作为 FAB 的直接父级,并使用 View 作为 FAB 的锚点。

那么app:layout_anchor="@id/app_bar_layout" 表示,这个FAB 锚定到ID 为“app_bar_layout”的视图。但您似乎没有任何 ID 为“app_bar_layout”的视图。

【讨论】:

  • dinesh 你所建议的就在那里,因为我没有为 Fragment 添加布局。
  • 好的。你能发布整个代码吗?我重申,CoordinatorLayout 必须是直接父级。
  • 我现在也更新了片段 xml。请看一看。
  • FAB 的父级是 FrameLayout。 FAB 的父级必须是 CoordinatorLayout ,只有这样 layout_anchor 才能工作。
  • 我尝试将它放在 Coordinator 中,但后来它隐藏在 BottomSheet 下方
猜你喜欢
  • 2017-08-07
  • 1970-01-01
  • 2012-11-15
  • 2022-01-27
  • 1970-01-01
  • 2017-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多