2016-06-17 26 views
2

私はプロジェクトにボトムシートビューを統合しました。私は、画面の高さの50%をクリックしてユーザーが親の高さにドラッグして引っ張ったり、下に崩壊することができますボタンをクリックしてボトムシートビューを開きたい。 ボトムシートAndroidを表示

は、私はそれを試してみましたが、私はボトムシート、それを開くためのボタンをクリックしたときに、親の完全な高さを取っています。

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); 


      } 
     }); 
    } 
+0

あなたは何を得ているのか、何を望んでいるのかをスクリーンショットで伝えることができます。だから私は理解するのが良い。 – Ironman

答えて

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> 
関連する問題