Android中Snackbar的介绍以及使用
Android中Snackbar的介绍以及使用
介绍
Snackbar可以说是Toast的升级版,不仅有显示信息的功能,还可以添加一个Action,实现点击功能,可以右滑删除。
效果图
Snackbar是Android Support Design Library库支持的一个控件,使用的时候需要一个控件容器用来容纳Snackbar.官方推荐使用CoordinatorLayout这个另一个Android Support Design Library库支持的控件容纳。因为使用这个控件,可以保证Snackbar可以让用户通过向右滑动退出。
XML布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
……
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
使用
在点击事件里添加弹出Snackbar
Snackbar.make(view, "您有新短消息,请注意查收。", Snackbar.LENGTH_INDEFINITE)
.setAction("点击查看", new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "TODO 查看消息", Toast.LENGTH_SHORT).show();
}
}).show();
方法介绍
- make()
public static Snackbar make(@NonNull View view, @NonNull CharSequence text, int duration)
和Toast使用方法类似,第一个参数是一个view,这里是CoordinatorLayout,第二个参数是提示的内容,第三个参数是显示的时常
Snackbar.LENGTH_INDEFINITE 是一直显示,只有右滑或者点击事件以后,可以移除
Snackbar.LENGTH_SHORT 和Toast的显示时长属性一样
Snackbar.LENGTH_LONG 和Toast的显示时长属性一样
- setAction()
public Snackbar setAction(CharSequence text, final OnClickListener listener)
第一个参数是可点击文字内容
第二个参数是文字的点击事件
- show()
public void show()
显示Snackbar
样式设定
修改Action字体颜色
public Snackbar setActionTextColor(@ColorInt int color)
使用的时候直接”点”出来设置一个颜色即可
Snackbar.make(view, "您有新短消息,请注意查收。", Snackbar.LENGTH_LONG)
.setAction("点击查看", new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "TODO 查看消息", Toast.LENGTH_SHORT).show();
}
}).setActionTextColor(Color.RED).show();
修改描述文字的颜色
Snackbar.class并没有给我们提供接口让我们来修改描述文字的字体颜色,如果一定要改也不是没有办法,可以获取TextView实例以后,修改字体颜色,然后再show()出来。
定义一个修改Snackbar描述文字颜色的方法(修改字体大小等属性也是同理,很简单,不一一举例了)
public void setSnackbarMessageTextColor(Snackbar snackbar, int color) {
View view = snackbar.getView();
((TextView) view.findViewById(R.id.snackbar_text)).setTextColor(color);
}
getView()是获取到Snackbar的布局,snackbar_text是描述文字的TextView的Id,没有为什么,源码就是这样定义的,下面是Snackbar的布局
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2015 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/snackbar_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingTop="@dimen/design_snackbar_padding_vertical"
android:paddingBottom="@dimen/design_snackbar_padding_vertical"
android:paddingLeft="@dimen/design_snackbar_padding_horizontal"
android:paddingRight="@dimen/design_snackbar_padding_horizontal"
android:textAppearance="@style/TextAppearance.Design.Snackbar.Message"
android:maxLines="@integer/design_snackbar_text_max_lines"
android:layout_gravity="center_vertical|left|start"
android:ellipsize="end"/>
<Button
android:id="@+id/snackbar_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/design_snackbar_extra_spacing_horizontal"
android:layout_marginStart="@dimen/design_snackbar_extra_spacing_horizontal"
android:layout_gravity="center_vertical|right|end"
android:paddingTop="@dimen/design_snackbar_padding_vertical"
android:paddingBottom="@dimen/design_snackbar_padding_vertical"
android:paddingLeft="@dimen/design_snackbar_padding_horizontal"
android:paddingRight="@dimen/design_snackbar_padding_horizontal"
android:visibility="gone"
android:textColor="?attr/colorAccent"
style="?attr/borderlessButtonStyle"/>
</merge><!-- From: file:/usr/local/google/buildbot/repo_clients/https___googleplex-android.googlesource.com_a_platform_manifest.git/mnc-sdk-release/frameworks/support/design/res/layout/design_layout_snackbar_include.xml -->
使用
Snackbar snackbar = Snackbar.make(view, "您有新短消息,请注意查收。", Snackbar.LENGTH_LONG)
.setAction("点击查看", new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "TODO 查看消息", Toast.LENGTH_SHORT).show();
}
}).setActionTextColor(Color.RED);
setSnackbarMessageTextColor(snackbar,Color.GREEN);
snackbar.show();
修改后的效果
Android中Snackbar的介绍以及使用的更多相关文章
- Android中,Broadcas介绍
什么是广播 在Android中,Broadcast是一种广泛运用的在应用程序之间传输信息的机制.我们拿广播电台来做个比方.我们平常使用收音机收音是这样的:许许多多不同的广播电台通过特定的频率来发送他们 ...
- android中Snackbar(Design Support)的使用
Snackbar是Android Design Support Library中的一个组件,想使用Snackbar,必须先引入Design Support,我这里引入的是当前的最新版本: implem ...
- Android中的Handler介绍
一.Handler的定义: 主要接受子线程发送的数据, 并用此数据配合主线程更新UI. 解释: 当应用程序启动时,Android首先会开启一个主线程 (也就是UI线程) , 主线程为管理界面中的UI控 ...
- Android 中常见控件的介绍和使用
1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...
- Android学习笔记之布局技巧以及布局中的细节介绍....
PS:休息两天,放一放手上的东西,做做总结... 学习内容: 1.Android中LinearLayout布局技巧... 2.layout中drawable属性的区别... 先简单的介绍一下dra ...
- Android中直播视频技术探究之---基础知识大纲介绍
一.前言 最近各种视频直播app到处都是,各种霸屏,当然我们也是需要体验的,关于视频直播的软件这里就不介绍了,在不是技术的人来看,直播是一种潮流,是一种娱乐方式,但是作为一个高技术的,我们除了看看,更 ...
- Android中FTP服务器、客户端搭建以及SwiFTP、ftp4j介绍
本文主要内容: 1.FTP服务端部署---- 基于Android中SwiFTP开源软件介绍: 2.FTP客户端部署 --- 基于ftp4j开源jar包的客户端开发 : 3.使用步骤 --- 如何测试我 ...
- Android中View和ViewGroup介绍
1. 概念Android中的View与我们以前理解的“视图”不同.在Android中,View比视图具有更广的含义,它包含了用户交互和显示,更像Windows操作系统中的window. ViewGro ...
- Android中图形截取的方式介绍
在Android的应用中,有时候我们想仅仅显示一部分图像,这时候就要求图形截图. 1.随意截取图像的方法,以下我们具体介绍一下android中的重要类--Bitmap public final cla ...
随机推荐
- [LeetCode] Toeplitz Matrix 托普利兹矩阵
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given ...
- [LeetCode] Set Intersection Size At Least Two 设置交集大小至少为2
An integer interval [a, b] (for integers a < b) is a set of all consecutive integers from a to b, ...
- 数据结构与算法 —— 链表linked list(04)
我们在上篇文章里面提到了链表的翻转,给定一个链表,对每两个相邻的节点作交换,并返回头节点,今天的这道题是它的升级版,如下: k个一组翻转链表 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链 ...
- java String的各种方法及操作
No. 方法名称 功能 字符与字符串 01 public String(char[] value) 将字符数组中所有内容变为字符串 02 public String(char[] value,int ...
- poj 1046 ——Color Me Less
提交地址:http://poj.org/problem?id=1046 Color Me Less Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- 安卓开发JAVA基础之初识JAVA
JAVA的一大特点------不依赖平台 JAVA在平台之上提供了一个JAVA运行环境(Java Runtime Environment, JRE),该环境由Java虚拟机(Java Virtua ...
- codeforces round #419 B. Karen and Coffee
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- poj 1113 凸包周长
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33888 Accepted: 11544 Descriptio ...
- mac电脑操作
1.在mac电脑上打开多个终端: command+n快捷键可以打开多个终端
- 你知道src、url、href的全称吗?
url:Uniform Resource Locator统一资源定位符 src:Source资源 href:Hypertext Reference超文本引用