底版带有版式膨胀或在屏幕顶部

时间:2018-06-21 12:09:58

标签: android material-design bottom-sheet

我想用布局膨胀设置底页。意味着我要在创建布局后以编程方式添加它。

我尝试过但没有成功。在下面的代码中编写,但不显示底页。

    View bottomSheet = getLayoutInflater().
            inflate(R.layout.custom_layout_search_pickup_location, coordinatorMain, false);


    // init the bottom sheet behavior
    bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);

    // change the state of the bottom sheet
    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);

请记住,我已经知道 BottomSheetBehavior 现在正在与 CoordinatorLayout 一起使用,并且必须在xml文件中添加布局。

已更新:我添加了用于说明的底部xml文件。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:gravity="center"
    android:orientation="vertical">

    <View
        android:layout_width="48dp"
        android:layout_height="2dp"
        android:background="@color/light_grey">

    </View>

    <View
        android:layout_width="48dp"
        android:layout_height="2dp"
        android:layout_marginTop="2dp"
        android:background="@color/light_grey">

    </View>
</LinearLayout>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="Where should we pick you up?"
    android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
    android:textStyle="bold" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:layout_margin="10dp"
    android:background="@drawable/bg_search_location">

    <ImageView
        android:id="@+id/imgViewPickUpMarker"
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:layout_centerVertical="true"
        android:layout_marginStart="10dp"
        app:srcCompat="@drawable/marker_pick_up" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginStart="10dp"
        android:padding="10dp"
        android:layout_toEndOf="@+id/imgViewPickUpMarker"
        android:layout_toStartOf="@+id/imgViewPickUpMarkerSearch"
        android:text="Search for pickup location" />

    <ImageView
        android:id="@+id/imgViewPickUpMarkerSearch"
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="10dp"
        android:layout_marginStart="10dp"
        app:srcCompat="@drawable/places_ic_search" />

</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:layout_margin="10dp">

    <ImageView
        android:id="@+id/imgViewMap"
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:layout_centerVertical="true"
        android:layout_marginStart="10dp"
        android:tint="@color/translucent_black"
        app:srcCompat="@android:drawable/ic_dialog_map" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginStart="10dp"
        android:layout_toEndOf="@+id/imgViewMap"
        android:text="SELECT VIA MAP"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:textColor="@color/translucent_black"
        android:textStyle="bold" />


</RelativeLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginEnd="10dp"
    android:layout_marginStart="20dp"
    android:background="@color/ash">

</View>

1 个答案:

答案 0 :(得分:0)

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

     <android.support.v7.widget.CardView
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:behavior_hideable="false"
            app:behavior_peekHeight="44dp"
            app:cardCornerRadius="0dp"
            app:cardElevation="5dp"
            app:layout_behavior="@string/bottom_sheet_behavior">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="360dp"
                android:orientation="vertical">

           </LinearLayout>
        </android.support.v7.widget.CardView>

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




 bottomSheetBehavior = BottomSheetBehavior.from(bottom_sheet);

        request_show_fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {

                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                } else {
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);

                }
            }
        });