一个可随意定位置的带色Toast——开源代码Crouton的简单使用
今天在公司要求的代码中,要求显示的提示能够更加具有多样化,而不是简单的Toast字样,第一想法肯定是自定义View呀,结果在浏览中发现还有这样的一个开源代码——Crouton。
几经折腾,发现这个东西还真是好用。不但可以给Toast置底色,还可以随意定义显示位置,而且还可以让你自己去自定义。
Demo代码已同步至:https://github.com/nanchen2251/CroutonDemo
上个简单的运行图
:
Crouton有三种底色:Alert(红色),Info(蓝色),Confirm(绿色),各种颜色可以通过Style指定。
由于这个开源库就是一个自定义View,所以不用去导成library,直接去github或者去我的github链接下载crouton包里面的类即可。
这是简单的包里面的内容:
我自己写这个Demo就相当简单了,我都不好意思发出来。
大家可以看看:
MainActivity.java
package com.example.nanchen.croutondemo; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout; import com.example.nanchen.croutondemo.crouton.Crouton;
import com.example.nanchen.croutondemo.crouton.Style; public class MainActivity extends AppCompatActivity { private LinearLayout layout; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); layout = (LinearLayout) findViewById(R.id.main_ll);
} public void topBtnClick(View view) {
Crouton.makeText(this,"显示顶部对话框", Style.INFO).show();
} public void otherBtnClick(View view) {
Crouton.makeText(this,"显示顶部对话框", Style.ALERT,layout).show();
}
}
然后是xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.nanchen.croutondemo.MainActivity"> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="topBtnClick"
android:text="显示顶部位置的提示框"/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="otherBtnClick"
android:text="显示其他位置的提示框"/> <LinearLayout
android:layout_marginTop="100dp"
android:id="@+id/main_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout> </LinearLayout>
然后运行就可以了。
当然这只是简单的使用,自定义视图肯定是可以的啦。
所以在代码上我们就去自定义一个带图片的提示框,上个运行图。

实现很简单,我自己写了一个other_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="Overdraw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f95063"
android:gravity="center_vertical"
android:orientation="horizontal"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:scaleType="center"/> <TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="这是提示"
android:textColor="#ffffff"/> </LinearLayout>
修改一下原来的xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.nanchen.croutondemo.MainActivity"> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="topBtnClick"
android:text="显示顶部位置的提示框"/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="otherBtnClick"
android:text="显示其他位置的提示框"/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myBtnClick"
android:text="显示自定义的提示框"/> <LinearLayout
android:layout_marginTop="100dp"
android:id="@+id/main_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout> </LinearLayout>
最后在主界面给这个按钮添加一个点击事件
/**
* 显示自定义的提示框
*/
public void myBtnClick(View view) {
View v = getLayoutInflater().inflate(R.layout.other_layout,null);
Crouton.make(this,v).show();
}
这里默认显示在顶部。
当然,这个开源库的功能不止如此,里面还可以通过setConfiguration( )来设置这个Crouton的配置信息,可以设置它的显示时长,而且可以设置它的点击事件等。
后续的大家自己去创新啦。
你们的点赞是我分享的动力,所以如果你开心,那就动动小手点个赞吧~~
一个可随意定位置的带色Toast——开源代码Crouton的简单使用的更多相关文章
- CF :K 一个含n条边的带权无向连通图,q次查询,每次查询两点间的最短距离。
题意:给你一个含n条边的带权无向连通图,q次查询,每次查询两点间的最短距离. 思路:LCA+思维. 设a,b两点间的距离为f(a,b) 则f(a,b)=dis[a]+dis[b]-2*dis[lca( ...
- C:\WINDOWS\system32\config\systemprofile\Desktop引用了一个不可用的位置
使用迅雷下载压缩文件到桌面时,下载完毕后,如果直接点击"打开文件",则迅雷会报错: C:\WINDOWS\system32\config\systemprofile\Desktop ...
- 从instr中截取第一个delimiter之前的内容放到outstr中,返回第一个delimiter之后的位置
从instr中截取第一个delimiter之前的内容放到outstr中,返回第一个delimiter之后的位置 char *msstrtok(char *instr, char *outstr, ch ...
- 使用ghost装完系统后出现“引用了一个不可用的位置”
用GHOST版光盘安装完系统后,只有一个C盘,无法点桌面刷新,然后提示“D:/我的文档引用了一个不可用的位置.... 1.在管理员账户下定位到如下键值:“HKEY_CURRENT_USER\Softw ...
- gvim写html代码时如何快速地跳转到一个标签的结束位置: 终极插件: matchit.vim
gvim写html代码时如何快速地跳转到一个标签的结束位置 参考这个vimrc的配置, 里面有一些 很好的东西, 配置很有用, 以前没有用到: http://www.cnblogs.com/wangj ...
- WPF 元素相对另外一个元素的 相对位置
原文:WPF 元素相对另外一个元素的 相对位置 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/koloumi/article/details/740 ...
- 小丁带你走进git世界一-git简单配置
小丁带你走进git世界一-git简单配置 1.github的简单配置 配置提交代码的信息,例如是谁提交的代码之类的. git config –global user.name BattleHeaer ...
- 动态生成一个设定好特殊样式的Tlabel,快速生成代码
动态生成一个设定好特殊样式的Tlabel,快速生成代码: 1.自己先在可视化界面设定一个Label,像这样: 2.选择label,快捷键ctrl+C 复制,粘贴带代码编辑器去,会生成一段这样的窗体代码 ...
- js 实现淘宝无缝轮播图效果,可更改配置参数 带完整版解析代码[slider.js]
前言: 本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽. 本篇文章为您分析一下原生JS写淘宝无缝轮播图效果 需求分析: ...
随机推荐
- mobx @computed的解读
写在前面:我一开始看不懂官网的@computed的作用,因为即使我把@computed去掉,依然能正确的report,然后我百度谷歌都找不到答案,下面都是我自己的理解,如果是有问题的,不对的,请务必留 ...
- nginx配置反向代理或跳转出现400问题处理记录
午休完上班后,同事说测试站点访问接口出现400 Bad Request Request Header Or Cookie Too Large提示,心想还好是测试服务器出现问题,影响不大,不过也赶紧上 ...
- web前端基础知识
#HTML 什么是HTML,和他ML... 网页可以比作一个装修好了的,可以娶媳妇的房子. 房子分为:毛坯房,精装修 毛坯房的修建: 砖,瓦,水泥,石头,石子.... 精 ...
- 8.仿阿里云虚拟云服务器的FTP(包括FTP文件夹大小限制)
平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html#iis 原文:http://dnt.dkill.net/Ar ...
- 【翻译】MongoDB指南/CRUD操作(二)
[原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(二) 主要内容: 更新文档,删除文档,批量写操作,SQL与MongoDB映射图,读隔离(读关 ...
- HttpUrlConnection 基础使用
From https://developer.android.com/reference/java/net/HttpURLConnection.html HttpUrlConnection: A UR ...
- 自己实现一个javascript事件模块
nodejs中的事件模块 nodejs中有一个events模块,用来给别的函数对象提供绑定事件.触发事件的能力.这个别的函数的对象,我把它叫做事件宿主对象(非权威叫法),其原理是把宿主函数的原型链指向 ...
- 使用C/C++写Python模块
最近看开源项目时学习了一下用C/C++写python模块,顺便把学习进行一下总结,废话少说直接开始: 环境:windows.python2.78.VS2010或MingW 1 创建VC工程 (1) 打 ...
- 【Reading Note】Python读书杂记
赋值 >>> list=[] >>> app=[list,list,list] >>> app [[], [], []] >>> ...
- Spark踩坑记——初试
[TOC] Spark简介 整体认识 Apache Spark是一个围绕速度.易用性和复杂分析构建的大数据处理框架.最初在2009年由加州大学伯克利分校的AMPLab开发,并于2010年成为Apach ...