android - 如何从 Material Design 文档中实现持久性 Bottom Sheets

标签 android material-design bottom-sheet

谁有或可以提供更多相关信息?只是搜索了一半的网络,但找不到更多的东西,甚至没有来自谷歌的演示应用程序。

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

我怎样才能实现这些持久性 Bottom Sheets?

最佳答案

这里有一个很棒的教程,介绍如何仅使用 Android 库(包括 Android 支持库)来完成此操作:

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

我会将代码复制并粘贴到此处以供后代使用。

在build.gradle中添加依赖:

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

res/layout/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/layout/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/layout/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/layout/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;
    }


}

主要 Activity .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;

        }
    }
}

关于android - 如何从 Material Design 文档中实现持久性 Bottom Sheets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32961529/

相关文章:

android - 搜索操作项未显示编辑控件

android - 具有可折叠工具栏的BottomSheetDialog

android - BottomSheetView 中的 animateLayoutChanges ="true"显示意外行为

android - 底部导航上方的 Bottom Sheet

android - 替换默认的未捕获异常处理程序以避免崩溃对话框

android - 将 excel 文件上传到 Dropbox 或 Google Drive?

android - UrbanAirship 崩溃的 Android 应用程序

android - Android SpeechRecognizer 类能否识别音节/音素而不仅仅是完整的单词?

android - 在 Material Design 中呈现的 Android Lollipop 半透明渐变状态栏

angularjs - angularjs Material Design 中的对话框弹出窗口如何返回 promise ?