Android Design Support Library 有哪些新功能以及如何使用它的 Snackbar?

What are the new features of Android Design Support Library and how to use its Snackbar?

Android 昨天发布了面向开发人员的 M 预览版。像往常一样,引入了许多惊人的新功能。我注意到 Snackbar 就是其中之一。

我已经阅读了关于Snackbar的文档,从中得知Snackbar在Android Design Support Library的库中,其绝对路径是android.support.design.widget.Snackbar

文档中说:

Snackbars provide lightweight feedback about an operation. They show a brief message at the bottom of the screen on mobile and lower left on larger devices. Snackbars appear above all other elements on screen and only one can be displayed at a time.

They automatically disappear after a timeout or after user interaction elsewhere on the screen, particularly after interactions that summon a new surface or activity. Snackbars can be swiped off screen.

那么,Snackbar 表现得像 Toast 还是 Dialog?可以在布局文件中使用 Snackbars 吗?我如何以编程方式使用它?

P.S.:

新的Snackbar不需要Android-M.

它位于新设计支持库中,您今天就可以使用它。

只需更新您的 SDK,在您的代码中添加此依赖项:

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

您可以使用这样的代码:

Snackbar.make(view, "Here's a Snackbar", Snackbar.LENGTH_LONG)
        .setAction("Action", null)
        .show();

就像吐司。

分配操作,您必须设置 OnClickListener

Snackbar.make(view, "Here's a Snackbar", Snackbar.LENGTH_LONG)
        .setAction("Action", myOnClickListener)
        .show();

如果您想更改背景颜色,您可以使用这样的东西:

 Snackbar snackbar = Snackbar.make(view, "Here's a Snackbar",       
                   Snackbar.LENGTH_LONG);
 View snackBarView = snackbar.getView();
 snackBarView.setBackgroundColor(colorId);
 snackbar.show();

如果您想要一些内置功能,例如 滑动关闭 手势,或 FAB 向上滚动小吃店,只是在您的视图层次结构中有一个 CoordinatorLayout

Activity 您可以使用:

String s = "SnackBar"
Snackbar.make(findViewById(android.R.id.content), s, Snackbar.LENGTH_LONG).show();

以及 Fragment

Snackbar.make(getView(), s, Snackbar.LENGTH_LONG).show();

编辑:

为了改变背景颜色,我使用这样的东西:

String s = "SnackBar"
Snackbar snack = Snackbar.make(getView(), s, Snackbar.LENGTH_LONG);
View view = snack.getView();
view.setBackgroundColor(Color.YELLOW); 
snack.show();

以及更改文本颜色(尽管主题):

View view = snack.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);

工作起来很有魅力 ;-)

至于Snackbar,它的作用类似于Toast,但与Toast不同。 Snackbars 显示在屏幕底部,包含带有可选单个操作的文本。它们会在给定的时间长度后通过动画关闭屏幕自动超时。此外,用户可以在超时前滑动它们,这比另一种轻量级反馈机制 toasts 更强大。

您可以像这样以编程方式使用它:

Snackbar snackbar = Snackbar
  .make(parentLayout, R.string.snackbar_text, Snackbar.LENGTH_LONG)
  .setAction(R.string.snackbar_action, myOnClickListener);
snackbar.setActionTextColor(Color.CYAN);
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.YELLOW);//change Snackbar's background color;
TextView textView = (TextView)snackbarView .findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.BLUE);//change Snackbar's text color;
snackbar.show(); // Don’t forget to show!

请注意在 make() 方法中使用视图 - Snackbar 将尝试找到它以确保它固定在底部。

此外,Android设计支持库用于Android 2.1+ (API 7+) ,具有抽屉式导航视图用于编辑文本的浮动标签浮动操作按钮, snackbar, tabs 和类似的东西。

导航视图

导航抽屉可以成为您应用程序中标识和导航的重要焦点,此处设计的一致性可以显着提高您的应用程序导航的容易程度,尤其是对于初次使用的用户。 NavigationView 通过提供导航抽屉所需的框架以及通过菜单资源扩充导航项的能力,使这变得更容易。

你可以这样使用:

<android.support.v4.widget.DrawerLayout
        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:fitsSystemWindows="true">
    <!-- your content layout -->
    <android.support.design.widget.NavigationView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/drawer_header"
            app:menu="@menu/drawer"/>
</android.support.v4.widget.DrawerLayout>

至于抽屉菜单,可以是:

<group android:checkableBehavior="single">
    <item
        android:id="@+id/navigation_item_1"
        android:checked="true"
        android:icon="@drawable/ic_android"
        android:title="@string/navigation_item_1"/>
    <item
        android:id="@+id/navigation_item_2"
        android:icon="@drawable/ic_android"
        android:title="@string/navigation_item_2"/>
</group>

或:

<item
    android:id="@+id/navigation_subheader"
    android:title="@string/navigation_subheader">
    <menu>
        <item
            android:id="@+id/navigation_sub_item_1"
            android:icon="@drawable/ic_android"
            android:title="@string/navigation_sub_item_1"/>
        <item
            android:id="@+id/navigation_sub_item_2"
            android:icon="@drawable/ic_android"
            android:title="@string/navigation_sub_item_2"/>
    </menu>
</item>

您将通过使用 setNavigationItemSelectedListener() 设置 OnNavigationItemSelectedListener 来获得所选项目的回调。这为您提供了被单击的 MenuItem,允许您处理选择事件、更改选中状态、加载新内容、以编程方式关闭抽屉或您可能需要的任何其他操作。

用于编辑文本的浮动标签

即使是不起眼的 EditText 在 material 设计上也有改进的空间。虽然仅 EditText 会在输入第一个字符后隐藏提示文本,但您现在可以将其包裹在 TextInputLayout 中,从而使提示文本变成 浮动标签 EditText 之上,确保用户在输入内容时不会丢失上下文。除了显示提示之外,您还可以通过调用 setError().

EditText 下方显示错误消息

浮动操作按钮

浮动操作按钮 是一个圆形按钮,表示界面上的主要操作。 Design 库的 FloatingActionButton 为您提供单一一致的实现,默认情况下使用主题中的 colorAccent 着色。

由于 FloatingActionButton 扩展了 ImageView,您将使用 android:src 或任何方法,例如 setImageDrawable() 来控制 [=28= 中显示的图标].

制表符

顶级导航模式 通常用于组织不同的内容分组。 Design 库的 TabLayout 既实现了固定选项卡,其中视图的宽度在所有选项卡之间平均分配,也实现了可滚动选项卡,其中选项卡的大小不统一,可以水平滚动。

可以通过编程方式添加选项卡:

TabLayout tabLayout = ...;
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));

如果您想使用 ViewPager 在选项卡之间进行水平分页,您可以直接从 PagerAdapter’s getPageTitle() 创建选项卡,然后使用 setupWithViewPager() 将两者连接在一起.这可确保选项卡选择事件更新 ViewPager 并且页面更改更新所选选项卡。

CoordinatorLayout 和应用栏

Design 库引入了 CoordinatorLayout,这是一种布局,可对 child 视图之间的触摸事件提供额外级别的控制,Design 库中的许多组件都利用了这一点。如果您尝试使用 AppBarLayout 允许您的 Toolbar 和其他视图(例如 TabLayout 提供的选项卡)对标有 ScrollingViewBehavior 的同级视图中的滚动事件做出反应。因此,您可以创建如下布局:

<android.support.design.widget.CoordinatorLayout
        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">

     <! -- Your Scrollable View -->
    <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
   <android.support.v7.widget.Toolbar
                  ...
                  app:layout_scrollFlags="scroll|enterAlways">

        <android.support.design.widget.TabLayout
                  ...
                  app:layout_scrollFlags="scroll|enterAlways">
     </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

现在,当用户滚动 RecyclerView 时,AppBarLayout 可以通过使用 children 的滚动标志来控制他们如何进入(在屏幕上滚动)来响应这些事件并退出(滚出屏幕)。

设计库、AppCompat 和所有 Android 支持库都是重要的工具,可提供构建现代、美观的 Android 应用程序所需的构建块,而无需从头开始构建所有内容。

10秒android做一个snackbar很简单

Snackbar.make(view, "Hello SnackBar", Snackbar.LENGTH_LONG)
                    .setAction("Its Roy", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        }
                    })
                    .setDuration(10000)
                    .setActionTextColor(getResources().getColor(R.color.colorAccent))
            .show();

我设置编码

Snackbar snackbar = Snackbar
        .make(getView(), text, Snackbar.LENGTH_INDEFINITE);

View sbView = Global.alert.getView();
sbView.setBackgroundColor(0xFF000000);

TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.WHITE);
textView.setTextSize(30);

Global.alert.show();

Snackbar 也可以显示图标。

例如:对于没有网络的情况,您可以像这样显示小吃栏(就像 Gmail 一样)。

SpannableStringBuilder builder = new SpannableStringBuilder();
    builder.append(" ").setSpan(new ImageSpan(this, R.drawable.snackbar_icon), 0, 1, 0);
    builder.append(" No Network Available");
    Snackbar snackBar = Snackbar.make(findViewById(R.id.co_ordinate), builder, Snackbar.LENGTH_LONG);
    snackBar.setAction("RETRY", new View.OnClickListener() {
        @Override
        public void onClick(View v) { //Retry Code here 

        }
    });
    snackBar.show();

你见过http://android-developers.blogspot.in/2015/05/android-design-support-library.html吗?

它总结了整个支持库。

您还可以更改小吃栏的文字颜色和背景

 Snackbar snackbar = Snackbar.make(view, "Text",
                            Snackbar.LENGTH_LONG);
                    View snackBarView = snackbar.getView();
                    TextView tv = (TextView) snackBarView.findViewById(android.support.design.R.id.snackbar_text);
                    tv.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.colorAccent));
                    snackBarView.setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimaryDark));
                    snackbar.show();