繁体   English   中英

在Android中从上到下打开底部对话框

[英]Open Bottom dialog from mid to top in Android

我希望我的底部对话框默认在中间高度打开,在滑动时,它应该打开到顶部。 Google地图将显示在上半部分,另一半显示在Bottomsheet ,当用户向上滑动底部时,它应该会向顶部打开。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:orientation="vertical"
             android:layout_height="match_parent"
             tools:context=".home.MainActivity">

<LinearLayout android:layout_width="match_parent"
              android:orientation="vertical"
              android:weightSum="2"
              android:layout_height="match_parent">

    <fragment
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/map"
            android:layout_weight="1"
            tools:context=".module.ride.activity.MapsActivity"
            android:name="com.google.android.gms.maps.SupportMapFragment"/>

    <LinearLayout android:layout_width="match_parent"
                  android:orientation="vertical"
                  android:layout_weight="1"
                  android:layout_height="0dp">

    </LinearLayout>

</LinearLayout>


<android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff"
            android:orientation="vertical"
            app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_gravity="center_vertical"
                android:weightSum="3">

            <ImageView android:layout_width="match_parent"
                       app:srcCompat="@drawable/ic_openclose"
                       android:id="@+id/imgExpand"
                       android:layout_height="@dimen/_40sdp"/>

        </LinearLayout>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Chicken Fried Rice 1x1"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Paneer Tikka 1x2"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Delivery Address"
                android:textColor="#444"
                android:textStyle="bold"/>

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Flat No 404, Skyline Apartments, Vizag - 500576"/>

        <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:background="#000"
                android:foreground="?attr/selectableItemBackground"
                android:text="PROCEED PAYMENT"
                android:textColor="#fff"/>

    </LinearLayout>

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

我的科特林代码

 var sheetBehavior: BottomSheetBehavior<*>? = null
 sheetBehavior = BottomSheetBehavior.from(bottom_sheet);
        (sheetBehavior as BottomSheetBehavior<*>?)!!.setBottomSheetCallback(object :
            BottomSheetBehavior.BottomSheetCallback() {
            override fun onSlide(p0: View, p1: Float) {

        }
        override fun onStateChanged(p0: View, p1: Int) {
            when (p0.id) {

                BottomSheetBehavior.STATE_HIDDEN -> {
                    System.out.println("----------STATE_HIDDEN")
                }
                BottomSheetBehavior.STATE_EXPANDED -> {
                    System.out.println("-------------STATE_EXPANDED")
                }
                BottomSheetBehavior.STATE_COLLAPSED -> {
                    System.out.println("-----------STATE_COLLAPSED")
                }
            }
        }
    })
    imgExpand.setOnClickListener() {
        if ((sheetBehavior as BottomSheetBehavior<*>?)!!.state != BottomSheetBehavior.STATE_EXPANDED) {
            (sheetBehavior as BottomSheetBehavior<*>?)!!.setState(BottomSheetBehavior.STATE_EXPANDED);
        } else {
            (sheetBehavior as BottomSheetBehavior<*>?)!!.setState(BottomSheetBehavior.STATE_COLLAPSED);
        }
    }
}

我尝试了一些东西,但显示出这样的东西

在此处输入图片说明

但是我想做这样的事情

在此处输入图片说明

将属性app:layout_behavior="android.support.design.widget.BottomSheetBehavior">LinearLayout移到工作表的根视图(即CoordinatorLayout

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

<LinearLayout
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:orientation="vertical">

为您的底页设置peekHeight应该可以帮助您获得所需的行为。 这是peekHeight的定义。

STATE_COLLAPSED :底部可见,但仅显示其窥视高度。 此状态通常是底表的“静止位置”。 窥视高度由开发人员选择,并且应足以指示有更多内容,允许用户触发操作或扩展底页。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM