如何从Material Design文档实现持久性底部工作表

时间:2015-10-06 04:04:27

标签: android persistent material

任何人有或可以提供更多相关信息?刚刚搜索了一半的网络,但找不到更多,甚至没有谷歌的演示应用程序。

https://www.google.com/design/spec/components/bottom-sheets.html#bottom-sheets-persistent-bottom-sheets

如何实现这些持久性底部表格?

2 个答案:

答案 0 :(得分:3)

这是一个很棒的教程,介绍如何使用Android库,包括Android支持库:

https://questdot.com/android-persistent-and-modal-bottom-sheet-tutorial/

我会将代码复制并粘贴给后人。

为build.gradle添加依赖项:

compile 'com.android.support:design:25.1.1'

RES /布局/ custom_bottom_sheet_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottomSheetLayout"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:background="@android:color/holo_red_light"
    android:padding="16dp"
    app:behavior_peekHeight="60dp"
    app:layout_behavior="@string/bottom_sheet_behavior">
    <!--  app:behavior_hideable="true"-->
    <TextView
        android:id="@+id/bottomSheetHeading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Any Title"
        android:textAppearance="@android:style/TextAppearance.Large"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="This is Modal Bottom Sheet Dialog"
        android:textAppearance="@android:style/TextAppearance.Large"
        android:layout_below="@+id/bottomSheetHeading"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="79dp" />
</RelativeLayout>

RES /布局/ content_bottom_sheet.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottomSheetLayout"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:background="@android:color/holo_blue_dark"
    android:padding="16dp"
    app:behavior_peekHeight="60dp"
    app:layout_behavior="@string/bottom_sheet_behavior">

    <TextView
        android:id="@+id/bottomSheetHeading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Expand Me"
        android:textAppearance="@android:style/TextAppearance.Large"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="This is Persistent Bottom Sheet"
        android:textAppearance="@android:style/TextAppearance.Large"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

RES /布局/ content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.questdot.bottomsheetexample.MainActivity"
    tools:showIn="@layout/activity_main">

    <Button
        android:id="@+id/show_bottom_sheet_dialog_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show Modal Bottom Sheet" />
</RelativeLayout>

RES /布局/ 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"
    android:fitsSystemWindows="true"
  >

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
      >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
           />

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

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

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


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

CustomBottomSheetDialogFragment.java:

import android.os.Bundle;
import android.support.design.widget.BottomSheetDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.custom_bottom_sheet_dialog, container, false);
        return v;
    }


}

MainActivity.java:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private BottomSheetBehavior bottomSheetBehavior;
    private Button showBottomSheetDialogButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        bottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.bottomSheetLayout));
        showBottomSheetDialogButton = (Button) findViewById(R.id.show_bottom_sheet_dialog_button);
        showBottomSheetDialogButton.setOnClickListener(this);

        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(View bottomSheet, int newState) {

                if (newState == BottomSheetBehavior.STATE_EXPANDED) {
                    //      bottomSheetHeading.setText(getString(R.string.text_collapse_me));
                } else {
                    //     bottomSheetHeading.setText(getString(R.string.text_expand_me));
                }

                switch (newState) {
                    case BottomSheetBehavior.STATE_COLLAPSED:
                        Log.e("Bottom Sheet Behaviour", "STATE_COLLAPSED");
                        break;
                    case BottomSheetBehavior.STATE_DRAGGING:
                        Log.e("Bottom Sheet Behaviour", "STATE_DRAGGING");
                        break;
                    case BottomSheetBehavior.STATE_EXPANDED:
                        Log.e("Bottom Sheet Behaviour", "STATE_EXPANDED");
                        break;
                    case BottomSheetBehavior.STATE_HIDDEN:
                        Log.e("Bottom Sheet Behaviour", "STATE_HIDDEN");
                        break;
                    case BottomSheetBehavior.STATE_SETTLING:
                        Log.e("Bottom Sheet Behaviour", "STATE_SETTLING");
                        break;
                }
            }


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

            }
        });



    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.show_bottom_sheet_dialog_button:
                new CustomBottomSheetDialogFragment().show(getSupportFragmentManager(), "Dialog");
                break;

        }
    }
}

答案 1 :(得分:1)

我今天必须这样做,在谷歌地图上做一些奇特的事情,并找到了一种方法:

  1. 导入this library

  2. 要允许触摸底部工作表以外而不解除它,请致电:

    bottomSheetLayout.setInterceptContentTouch(false);
    
  3. 删除调光效果,请致电:

     bottomSheetLayout.setShouldDimContentView(false);