谷歌的BottomSheet出现在屏幕顶部

5
我将使用Android的Design Library里的新BottomSheet。 问题是我将其用于Fragment中,导致它出现在屏幕顶部而不是底部。
这是我的Activity xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="ir.aidinsoft.quicktaxi.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/tlbr_acMain"
        android:elevation="5dp"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/crdl_acMain"
        android:layout_below="@+id/tlbr_acMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nscv_acMain"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <!-- This Layout Is Replacement Layout For Fragment -->
            <RelativeLayout
                android:id="@+id/frml_acMain"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </android.support.v4.widget.NestedScrollView>

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

</RelativeLayout>

这是我的Fragment xml:

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ir.aidinsoft.quicktaxi.MyFragment">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <io.codetail.widget.RevealFrameLayout
            android:id="@+id/rvfl_frTaxi"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="invisible">

            <fragment
                android:id="@+id/frag_frTaxi_map"
                android:name="com.google.android.gms.maps.SupportMapFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </io.codetail.widget.RevealFrameLayout>

    </RelativeLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/facb_frTaxi_request"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!-- My Bottom Sheet Layout -->
    <FrameLayout
        android:id="@+id/frml_frTaxi_bottomSheet"
        android:layout_width="match_parent"
        android:layout_height="360dp"
        android:background="@color/colorPrimaryLight"
        app:behavior_hideable="true"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior" />

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

所以,使用以下代码在布局中替换片段:

getSupportFragmentManager().beginTransaction().replace(R.id.frml_acMain,  taxiFragment).commit();

我添加了以下代码,以便在FAB点击状态下显示BottomSheet。 片段的Java代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment, container, false);
    fab = (FloatingActionButton) view.findViewById(R.id.facb_frTaxi_request);
    bottomSheet = view.findViewById(R.id.frml_frTaxi_bottomSheet);
    BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
    return view;
}

@Override
public void onResume() {
    super.onResume();
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    });
}

问题出在哪里?我做错了什么吗? TnQ


你找到解决方案了吗? - Firat
1
在我的情况下,这是由于animateLayoutChanges动画引起的。 - Saman Sattari
1
@SamanSattari 你可能想考虑将其作为答案添加到你的问题中。我刚遇到了同样的问题,删除 animateLayoutChanges 对我有帮助。 - ashu
1个回答

2

如果你在布局中使用了animateLayoutChanges,那么将它移除可以解决你的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接