开源代码——Crouton

一个可随意定位置的带色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

 1 package com.example.nanchen.croutondemo;
2
3 import android.support.v7.app.AppCompatActivity;
4 import android.os.Bundle;
5 import android.view.View;
6 import android.widget.LinearLayout;
7
8 import com.example.nanchen.croutondemo.crouton.Crouton;
9 import com.example.nanchen.croutondemo.crouton.Style;
10
11 public class MainActivity extends AppCompatActivity {
12
13 private LinearLayout layout;
14
15 @Override
16 protected void onCreate(Bundle savedInstanceState) {
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.activity_main);
19
20 layout = (LinearLayout) findViewById(R.id.main_ll);
21 }
22
23 public void topBtnClick(View view) {
24 Crouton.makeText(this,"显示顶部对话框", Style.INFO).show();
25 }
26
27 public void otherBtnClick(View view) {
28 Crouton.makeText(this,"显示顶部对话框", Style.ALERT,layout).show();
29 }
30 }

然后是xml

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 xmlns:tools="http://schemas.android.com/tools"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 android:orientation="vertical"
8 tools:context="com.example.nanchen.croutondemo.MainActivity">
9
10
11 <Button
12 android:layout_width="match_parent"
13 android:layout_height="wrap_content"
14 android:onClick="topBtnClick"
15 android:text="显示顶部位置的提示框"/>
16
17 <Button
18 android:layout_width="match_parent"
19 android:layout_height="wrap_content"
20 android:onClick="otherBtnClick"
21 android:text="显示其他位置的提示框"/>
22
23 <LinearLayout
24 android:layout_marginTop="100dp"
25 android:id="@+id/main_ll"
26 android:layout_width="match_parent"
27 android:layout_height="wrap_content"
28 android:orientation="horizontal">
29 </LinearLayout>
30
31 </LinearLayout>

然后运行就可以了。

当然这只是简单的使用,自定义视图肯定是可以的啦。

所以在代码上我们就去自定义一个带图片的提示框,上个运行图。

实现很简单,我自己写了一个other_layout.xml

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 xmlns:tools="http://schemas.android.com/tools"
5 tools:ignore="Overdraw"
6 android:layout_width="match_parent"
7 android:layout_height="wrap_content"
8 android:background="#f95063"
9 android:gravity="center_vertical"
10 android:orientation="horizontal">
11
12 <ImageView
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content"
15 android:src="@mipmap/ic_launcher"
16 android:scaleType="center"/>
17
18
19 <TextView
20 android:id="@+id/tv_content"
21 android:layout_width="wrap_content"
22 android:layout_height="wrap_content"
23 android:layout_margin="10dp"
24 android:text="这是提示"
25 android:textColor="#ffffff"/>
26
27 </LinearLayout>

修改一下原来的xml文件

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 xmlns:tools="http://schemas.android.com/tools"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 android:orientation="vertical"
8 tools:context="com.example.nanchen.croutondemo.MainActivity">
9
10
11 <Button
12 android:layout_width="match_parent"
13 android:layout_height="wrap_content"
14 android:onClick="topBtnClick"
15 android:text="显示顶部位置的提示框"/>
16
17 <Button
18 android:layout_width="match_parent"
19 android:layout_height="wrap_content"
20 android:onClick="otherBtnClick"
21 android:text="显示其他位置的提示框"/>
22
23 <Button
24 android:layout_width="match_parent"
25 android:layout_height="wrap_content"
26 android:onClick="myBtnClick"
27 android:text="显示自定义的提示框"/>
28
29 <LinearLayout
30 android:layout_marginTop="100dp"
31 android:id="@+id/main_ll"
32 android:layout_width="match_parent"
33 android:layout_height="wrap_content"
34 android:orientation="horizontal">
35 </LinearLayout>
36
37 </LinearLayout>

最后在主界面给这个按钮添加一个点击事件

1
2
3
4
5
6
7
/**
    * 显示自定义的提示框
    */
   public void myBtnClick(View view) {
       View v = getLayoutInflater().inflate(R.layout.other_layout,null);
       Crouton.make(this,v).show();
   }

  

这里默认显示在顶部。

当然,这个开源库的功能不止如此,里面还可以通过setConfiguration( )来设置这个Crouton的配置信息,可以设置它的显示时长,而且可以设置它的点击事件等。

后续的大家自己去创新啦。

你们的点赞是我分享的动力,所以如果你开心,那就动动小手点个赞吧~~

 
分类: android

开源代码——Crouton的更多相关文章

  1. 一个可随意定位置的带色Toast——开源代码Crouton的简单使用

    今天在公司要求的代码中,要求显示的提示能够更加具有多样化,而不是简单的Toast字样,第一想法肯定是自定义View呀,结果在浏览中发现还有这样的一个开源代码——Crouton. 几经折腾,发现这个东西 ...

  2. GitHub + VSTS 开源代码双向同步

    GitHub已经是全球开源代码的大本营了,通过以下统计你可以看到仅仅javascript在github就有超过32万个活动的repo.很多开发人员都会把自己的一部分代码分享到github上进行开源,一 ...

  3. CWMP开源代码研究5——CWMP程序设计思想

    声明:本文涉及的开源程序代码学习和研究,严禁用于商业目的. 如有任何问题,欢迎和我交流.(企鹅号:408797506) 本文介绍自己用过的ACS,其中包括开源版(提供下载包)和商业版(仅提供安装包下载 ...

  4. iOS流行的开源代码库

    本文介绍一些流行的iOS的开源代码库 1.AFNetworking 更新频率高的轻量级的第三方网络库,基于NSURL和NSOperation,支持iOS和OSX.https://github.com/ ...

  5. CWMP开源代码研究2——easycwmp安装和学习

    声明:本文是对开源程序代码学习和研究,严禁用于商业目的. 如有任何问题,欢迎和我交流.(企鹅号:408797506) 本文所有笔记和代码可以到csdn下载:http://download.csdn.n ...

  6. CWMP开源代码研究1——开篇之作

    原创作品,转载请注明出处,严禁非法转载.如有错误,请留言! email:40879506@qq.com 声明:本系列涉及的开源程序代码学习和研究,严禁用于商业目的. 如有任何问题,欢迎和我交流.(企鹅 ...

  7. 开源代码Window下搭建rtmp流媒体服务器

    合肥程序员群:49313181. 合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入) Q Q:408365330 E-Mail:egojit@qq.com 综合:有这样需求,将摄像头 ...

  8. AgileEAS.NET SOA 中间件平台 5.2 发布说明-包含Silverlight及报表系统的开源代码下载

    一.AgileEAS.NET SOA 中间件简介      AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速 ...

  9. 苹果刷机相关开源代码(如iRecovery等)收集汇总(不断更新中...)

    下面截图是在下面开源代码下使用VS2015修改部分代码后适配而成,可以在Windows平台上运行, 下载连接: http://pan.baidu.com/s/1i4zKGx3.

随机推荐

  1. JavaMail简单接收邮件

    一个简单的例子,收取所有邮件并在控制台输出. package cn.jmail.test; import java.io.*; import java.util.*; import javax.mai ...

  2. IE10以下placeholder不兼容

    做页面的时候在谷歌中是显示的,但是换了IE之后总是不显示,一个文本框还好,如果有多个的话,如图: 添加以下一段Jquery代码: <script> var JPlaceHolder = { ...

  3. 记微信开发(有道翻译api)

    记微信开发(有道翻译api) 记微信开发(有道翻译api) 效果: 有道翻译api申请: 地址:http://fanyi.youdao.com/openapi code: <?php/** * ...

  4. Unix环境下PS1变量的设置

    我的ps1命令提示符: export PS1="\[\e[31;1m\]\u @ \[\e[34;1m\]\h \[\e[36;1m\]\w \[\e[33;1m\]\t $ \[\e[37 ...

  5. tokyocabinet安装日志(持续更新)

    http://sourceforge.jp/projects/sfnet_tokyocabinet/releases/这个网站的最新tt和tc都在此1.下载tokyocabinethttp://sou ...

  6. TypeError: 'QueryDict' object is not callable

    id = int(request.POST('id')) Error message: TypeError: 'QueryDict' object is not callable Error rese ...

  7. Java连接Azure SQL Database

    Azure SQL Database是Azure上的数据库PAAS服务,让用户可以快速的创建和使用SQL数据库而不用担心底层的备份,安全,运维,恢复等繁琐的工作,本文简单介绍如何使用Java程序连接到 ...

  8. 新版703n刷openwrt

    自带的官方固件: 3.17.1 Build 140120 Rel.56593n WR703N v1 00000000 在不能web页面刷固件,因为带了校验功能. 老外给出了不上TTL刷路由的方法: h ...

  9. Inno Setup设定只运行一个安装包

    原文 http://zwkufo.blog.163.com/blog/static/25882512010292526944/?suggestedreading&wumii 在安装包中,经常会 ...

  10. index seek与index scan

    原文地址:http://blog.csdn.net/pumaadamsjack/article/details/6597357 低效Index Scan(索引扫描):就全扫描索引(包括根页,中间页和叶 ...