2

我在我的项目中集成了底页视图。我想在按钮单击时打开底部工作表视图到屏幕高度的 50%,然后在用户可以通过拖动到父高度或折叠到底部后将其拉动。

我试过了,但是当我单击按钮打开它时,底部的工作表占据了父级的全部高度。

XML 代码:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    android:id="@+id/mainOuterLinearLayout"
    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">

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

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/mainOuterLayout"
        android:layout_width="match_parent"
        android:layout_height="fill_parent">

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

            <LinearLayout
                android:id="@+id/linearLayoutOverFlowButtons"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal"
                android:paddingBottom="0dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:paddingTop="5dp"
                android:weightSum="5">


                <Button
                    android:id="@+id/buttonDownloadDocument"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginLeft="5dp"
                    android:layout_weight="2"
                    android:background="@drawable/button_shape"
                    android:clickable="false"
                    android:minHeight="35dp"
                    android:minWidth="45dp"
                    android:text="Download Document"
                    android:textColor="@color/white"
                    android:textSize="12dp"/>

                <Button
                    android:id="@+id/buttonRequestDocument"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:layout_weight="2"
                    android:background="@drawable/button_shape"
                    android:clickable="false"
                    android:minHeight="35dp"
                    android:minWidth="45dp"
                    android:text="Request Document"
                    android:textColor="@color/white"
                    android:textSize="12dp"/>

                <Button
                    android:id="@+id/buttonFeedBack"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginRight="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/button_shape"
                    android:clickable="false"
                    android:minHeight="35dp"
                    android:minWidth="45dp"
                    android:text="Feedback"
                    android:textColor="@color/white"
                    android:textSize="12dp"/>

            </LinearLayout>
        </LinearLayout>

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:behavior_peekHeight="300dp"
            android:background="@android:color/holo_orange_light"
            android:clipToPadding="true"
            app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="16dp"
                android:text="Test"
                android:textSize="16sp"/>

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

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

代码:

private void addBottomSheetCode() {
        //CoordinatorLayout coordinatorLayout = (CoordinatorLayout) findViewById(R.id.mainOuterLayout);
        // The View with the BottomSheetBehavior
        bottom_sheet = mainOuterLayout.findViewById(R.id.bottom_sheet);
        behavior = BottomSheetBehavior.from(bottom_sheet);
        behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
                    behavior.setPeekHeight(0);
                }
            }

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


private void onClickButtonFeedBack() {
        buttonFeedBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DisplayMetrics displaymetrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
                int height = displaymetrics.heightPixels;
                int width = displaymetrics.widthPixels;

                /*bottom_sheet.getLayoutParams().height = height/2;
                bottom_sheet.requestLayout();*/
                behavior.setPeekHeight(300);
                behavior.setState(BottomSheetBehavior.STATE_EXPANDED);


            }
        });
    }
4

1 回答 1

0

请在下面找到更新的代码不起作用:

private void onClickButtonFeedBack() {
            buttonFeedBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DisplayMetrics displaymetrics = new DisplayMetrics();
                    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
                    int height = displaymetrics.heightPixels;
                    int width = displaymetrics.widthPixels;

                    int mheight = height - getSupportActionBar().getHeight();
                    behavior.setPeekHeight(mheight);
                    behavior.setState(BottomSheetBehavior.STATE_EXPANDED);


                }
            });

布局

<android.support.v4.widget.NestedScrollView
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="@color/bottom_sheet_view_color"
            android:clipToPadding="true"
            app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="16dp"
                android:text="Test"
                android:textSize="16sp"/>

        </android.support.v4.widget.NestedScrollView>
于 2016-06-17T10:55:00.770 回答