Android学习之Snackbar使用文档翻译

Snackbar出来很久了,在我的理解就是取代Toast的,它显示在屏幕底部,更简洁,还提供了点击事件

看了这篇很简单,就翻译一下,大家不要吐槽

文章来源:

http://guides.codepath.com/android/Displaying-the-Snackbar#overview

Snackbars用于展示在屏幕底部,包含一个文本和单击事件,会自动退出当时间超出近似toast的时间。Snackbars 可以通过用户或者其他的动作让它比简单的Toast更具活力,然后,api很相似。

Snackbars are shown on the bottom of the screen and contain text with an optional single action. They automatically fade out after enough time similar to a toast. Snackbars can be swiped away by the user or contain other actions making them more powerful than simple toasts. However, the API is very familiar.

Android学习之Snackbar使用文档翻译_第1张图片

现在Snackbars在设备底部,文本在右边

简单的Snackbars

确保支持Design Support Library 是第一步。创建Snackbars用make,设置显示用.show().

Make sure to follow the Design Support Library setup instructions first.

Create a snackbar using make , setting an optional action and then call .show() :

Snackbar.make(parentView, R.string.snackbar_text, Snackbar.LENGTH_LONG)
  .setAction(R.string.snackbar_action, myOnClickListener)
  .show(); // Don’t forget to show!

跟Toast很相似。

不同于Toast的一个地方是第一个参数是一个View,而不是Context。Snackbar用这个参数走父层次的检索CoordinatorLayout, FrameLayout,或者更顶端的布局。无论谁先来,添加CoordinatorLayout会对浮动按钮更有帮助性,按钮需要移动来创造空间来展示Snackbar

在最近更新支持库中,你可以指定LENGTH_INDEFINITE来持续展示Snackbar直到它消失或者另外一个展示

One difference from using Toasts from Snackbars is that the first parameter requires a View instead of Context. The snackbar uses this parameter to walk up the parent’s hierarchy searching for a CoordinatorLayout, FrameLayout, or the top-most container layout, whichever comes first. Adding a CoordinatorLayout in the view hierarchy is helpful in cases where the floating action buttons needs to moved to make room for displaying the Snackbar as discussed in this guide.

In a recent update of the support library, you can now specify LENGTH_INDEFINITE that will continue to show the Snackbar until it is dismissed or another one is shown:

Snackbar.make(parentView, R.string.snackbar_text, Snackbar.LENGTH_INDEFINITE).show();

配置选项

附加选项被用于配置snackbar例如setActionTextColor 和setDuration

Additional options can be used to configure the snackbar such a setActionTextColor and setDuration :

Snackbar.make(parentView, R.string.snackbar_text, Snackbar.LENGTH_LONG)
 .setAction(R.string.snackbar_action, myOnClickListener)  // action text on the right side
 .setActionTextColor(R.color.green)
 .setDuration(3000).show();

这就是全部

引用:

http://android-developers.blogspot.com/2015/05/android-design-support-library.html
http://developer.android.com/reference/android/support/design/widget/Snackbar.html
http://www.google.com/design/spec/components/snackbars-toasts.html

你可能感兴趣的:(android,Snackbar)