今天在公司要求的代码中,要求显示的提示能够更加具有多样化,而不是简单的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的简单使用的更多相关文章

  1. CF :K 一个含n条边的带权无向连通图,q次查询,每次查询两点间的最短距离。

    题意:给你一个含n条边的带权无向连通图,q次查询,每次查询两点间的最短距离. 思路:LCA+思维. 设a,b两点间的距离为f(a,b) 则f(a,b)=dis[a]+dis[b]-2*dis[lca( ...

  2. C:\WINDOWS\system32\config\systemprofile\Desktop引用了一个不可用的位置

    使用迅雷下载压缩文件到桌面时,下载完毕后,如果直接点击"打开文件",则迅雷会报错: C:\WINDOWS\system32\config\systemprofile\Desktop ...

  3. 从instr中截取第一个delimiter之前的内容放到outstr中,返回第一个delimiter之后的位置

    从instr中截取第一个delimiter之前的内容放到outstr中,返回第一个delimiter之后的位置 char *msstrtok(char *instr, char *outstr, ch ...

  4. 使用ghost装完系统后出现“引用了一个不可用的位置”

    用GHOST版光盘安装完系统后,只有一个C盘,无法点桌面刷新,然后提示“D:/我的文档引用了一个不可用的位置.... 1.在管理员账户下定位到如下键值:“HKEY_CURRENT_USER\Softw ...

  5. gvim写html代码时如何快速地跳转到一个标签的结束位置: 终极插件: matchit.vim

    gvim写html代码时如何快速地跳转到一个标签的结束位置 参考这个vimrc的配置, 里面有一些 很好的东西, 配置很有用, 以前没有用到: http://www.cnblogs.com/wangj ...

  6. WPF 元素相对另外一个元素的 相对位置

    原文:WPF 元素相对另外一个元素的 相对位置 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/koloumi/article/details/740 ...

  7. 小丁带你走进git世界一-git简单配置

    小丁带你走进git世界一-git简单配置 1.github的简单配置 配置提交代码的信息,例如是谁提交的代码之类的. git config  –global user.name BattleHeaer ...

  8. 动态生成一个设定好特殊样式的Tlabel,快速生成代码

    动态生成一个设定好特殊样式的Tlabel,快速生成代码: 1.自己先在可视化界面设定一个Label,像这样: 2.选择label,快捷键ctrl+C 复制,粘贴带代码编辑器去,会生成一段这样的窗体代码 ...

  9. js 实现淘宝无缝轮播图效果,可更改配置参数 带完整版解析代码[slider.js]

    前言:         本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽.         本篇文章为您分析一下原生JS写淘宝无缝轮播图效果 需求分析: ...

随机推荐

  1. C++对C的函数拓展

    一,内联函数 1.内联函数的概念 C++中的const常量可以用来代替宏常数的定义,例如:用const int a = 10来替换# define a 10.那么C++中是否有什么解决方案来替代宏代码 ...

  2. 简单有效的kmp算法

    以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...

  3. 从备考PMP到与项目经理同呼吸

    前言 PMP是什么梗? 项目管理专业人士资格认证.它是由美国项目管理协会(Project Management Institute(PMI)发起的,严格评估项目管理人员知识技能是否具有高品质的资格认证 ...

  4. 让kindeditor显示高亮代码

    kindeditor4.x代码高亮功能默认使用的是prettify插件,prettify是Google提供的一款源代码语法高亮着色器,它提供一种简单的形式来着色HTML页面上的程序代码,实现方式如下: ...

  5. 初学者看过来之JSON入门

    1. 什么是JSON JSON---Javascript Object Notation,前两个单词大家应该都认识,最后一个notation,是"记号.标记法"的意思,连在一起,便 ...

  6. win10上部署Hadoop-2.7.3——非Cygwin、非虚拟机

    开始接触Hadoop,听人说一般都是在Lunix下部署Hadoop,但是本人Lunix不是很了解,所以Google以下如何在Win10下安装Hadoop(之后再在Lunix下弄),找到不少文章,以下是 ...

  7. Configure a VLAN (on top of a bond) with NetworkManager (nmcli) in RHEL7

    not on top of a bond Environment Red Hat Enterprise Linux 7 NetworkManager Issue Need an 802.1q VLAN ...

  8. Linux学习

    Linux 命令英文全称su:Swith user 切换用户,切换到root用户cat: Concatenate 串联uname: Unix name 系统名称df: Disk free 空余硬盘du ...

  9. Linux设备管理(四)_从sysfs回到ktype

    sysfs是一个基于ramfs的文件系统,在2.6内核开始引入,用来导出内核对象(kernel object)的数据.属性到用户空间.与同样用于查看内核数据的proc不同,sysfs只关心具有层次结构 ...

  10. Hacker Rank: Two Strings - thinking in C# 15+ ways

    March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...