Android Buttom表无效

时间:2016-04-19 04:41:13

标签: android bottom-sheet

Android底部工作表无效。 下面是我的代码,我想创建底部演示文稿,但它不起作用,底部工作表没有显示。

    public class MainActivity extends AppCompatActivity  implements View.OnClickListener{
    private BottomSheetBehavior mBottomSheetBehavior;
    private Button button1;
    private View bottomSheet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getId();
        setListner();
        mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
    }

    private void getId() {
        try {
            button1 = (Button)findViewById(R.id.button1);
            bottomSheet = findViewById(R.id.bottom_sheet);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void setListner() {
        try {
            button1.setOnClickListener(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onClick(View v)
    {
        try {
            switch (v.getId())
            {
                case R.id.button1:
                mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);

                    Log.e("Buttom clicked",">>");
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这是我的xml文件。

        <android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="24dp">

            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Button 1"
                android:padding="16dp"
                android:layout_margin="8dp"
                android:textColor="@android:color/white"
                android:background="@android:color/holo_green_dark"/>
        </LinearLayout>
    </ScrollView>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:clipToPadding="true"
        android:background="@android:color/holo_orange_light"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
        >
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/ic_launcher"
            android:padding="16dp"
            android:textSize="16sp"/>
    </android.support.v4.widget.NestedScrollView>

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

实际上我想使用Android Support Library 23.2使用abouve代码实现底页而不使用任何其他库,但它无效。 谢谢和提前。

1 个答案:

答案 0 :(得分:1)

使用此依赖项编译'com.cocosw:bottomsheet:1.2.0'

并添加此代码

new BottomSheet.Builder(getActivity(), R.style.BottomSheet_StyleDialog).
                    sheet(R.menu.bottom_sheet).title(displayname).listener(new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                        case R.id.btnOne:
                            // do something...
                            break;
                        case R.id.btnTwo:
                           // do something ...
                                  }
                             } 
            }).show();