[Android Design Lib]Snackbar学习

参考文章:http://www.open-open.com/lib/view/open1433496206666.html

Snackbar是Toast的升级版,与Toast相比,它额外提供了show和dismiss的判断方法和回调接口。另外,它还提供了增加Button点击的机会,为我们省去了一些需要自定义view才能实现的类似Toast展示效果的提示浮层的工作。

1.首先,引入Android Design Support Library,Android Studio中通过配置依赖来引入:

//build.gradle中的dependencies中增加:
compile 'com.android.support:design:23.4.0'
2.Snackbar的展示效果如图:


它的位置固定,固定出现在屏幕的底部。左侧文案+右侧Action;右侧Action没有时,文案也依然显示在左侧不变。

3.代码调用如下:

                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                            }
                        })
                        .setCallback(new Snackbar.Callback() {
                            @Override
                            public void onDismissed(Snackbar snackbar, int event) {
                                super.onDismissed(snackbar, event);
                                Log.e("chwn","Snackbar dismiss");
                            }

                            @Override
                            public void onShown(Snackbar snackbar) {
                                super.onShown(snackbar);
                                Log.e("chwn","Snackbar show");
                            }
                        })
                        .show();

4.另外,它的Api接口如下:

从中可以看出,除了可以想Toast一样定制文本的内容外,还可以定制Action的文本及文字颜色。

详见:https://developer.android.com/reference/android/support/design/widget/Snackbar.html

Public methods

void dismiss()

Dismiss the Snackbar.

int getDuration()

Return the duration.

View getView()

Returns the Snackbar's view.

boolean isShown()

Return whether this Snackbar is currently being shown.

boolean isShownOrQueued()

Returns whether this Snackbar is currently being shown, or is queued to be shown next.

staticSnackbar make(View view, CharSequence text, int duration)

Make a Snackbar to display a message

Snackbar will try and find a parent view to hold Snackbar's view from the value given to view.

staticSnackbar make(View view, int resId, int duration)

Make a Snackbar to display a message.

Snackbar setAction(int resId, View.OnClickListener listener)

Set the action to be displayed in this Snackbar.

Snackbar setAction(CharSequence text, View.OnClickListener listener)

Set the action to be displayed in this Snackbar.

Snackbar setActionTextColor(ColorStateList colors)

Sets the text color of the action specified in setAction(CharSequence, View.OnClickListener).

Snackbar setActionTextColor(int color)

Sets the text color of the action specified in setAction(CharSequence, View.OnClickListener).

Snackbar setCallback(Snackbar.Callback callback)

Set a callback to be called when this the visibility of this Snackbar changes.

Snackbar setDuration(int duration)

Set how long to show the view for.

Snackbar setText(int resId)

Update the text in this Snackbar.

Snackbar setText(CharSequence message)

Update the text in this Snackbar.

void show()

Show the Snackbar.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值