2017-06-08 34 views
1

我正在试图制作一个底部工作表的应用程序。但是当我运行应用程序时,底部工作表显示在主屏幕上。这意味着底部工作表出现在主屏幕上,而不点击按钮。为了清楚理解,以下图像已被证实。 sample image底部工作表为什么在主屏幕上?

这是MainActivity.java类:

package com.example.qurdadzze.b; 
import android.support.design.widget.BottomSheetBehavior; 
import android.support.v4.widget.NestedScrollView; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends AppCompatActivity { 
Button btn; 
NestedScrollView bottomsheet; 
BottomSheetBehavior behavior; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    btn = (Button) findViewById(R.id.btn); 
    bottomsheet = (NestedScrollView) findViewById(R.id.bottomsheet); 
    behavior = BottomSheetBehavior.from(bottomsheet); 
    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(behavior.getState()==BottomSheetBehavior.STATE_COLLAPSED) 
       behavior.setState(BottomSheetBehavior.STATE_EXPANDED); 
      else 
       behavior.setState(BottomSheetBehavior.STATE_COLLAPSED); 
      behavior.setPeekHeight(0); 
     } 
    }); 
} 

这是activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    tools:context="com.example.qurdadzze.b.MainActivity"> 

    <Button 
     android:id="@+id/btn" 
     android:layout_width="150dp" 
     android:layout_height="50dp" 
     android:text="show bottom sheet"/> 

    <android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="300dp" 
     android:background="#99cc99" 
     android:elevation="100dp" 
     android:id="@+id/bottomsheet" 
     app:layout_behavior="@string/bottom_sheet_behavior"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textSize="20sp" 
      android:layout_gravity="center|top" 
      android:gravity="center" 
      android:text="it is me Bottom Sheet"/> 


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

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

回答

相关问题