BottomSheet查看Android

时间:2016-06-17 09:55:43

标签: android bottom-sheet

我在项目中集成了底部视图。我想打开按钮点击到屏幕高度的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);


            }
        });
    }

1 个答案:

答案 0 :(得分: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>