开源代码——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. PHP_CURL请求教程, 内含简单粗暴curl

    //curl访问 //需要url或者data //返回的数组是JSON数据形式 function ppd_curl($url,$data = null){ //\Think\Log::record($ ...

  2. hdu 5823 color II 状压dp

    题目链接 给n个点 n<=18. 然后给出它们两两之间是否有边相连. 问你这个图的所有子集,最少要用多少种颜色来染色, 如果两个点相连, 那么这两个点不能染同样的颜色. 先预处理出所有的点独立集 ...

  3. python爬图

    闲的无事,看着知乎里种种python优点,按捺不住,装起python3.4. 网上找了点爬行图片的代码,修改至兼容3.4,成功爬行指定url所有jpg图片,代码段如下: import os impor ...

  4. pyinstaller打出的EXE包执行时报错“failed to excute ”信息

    我的程序是selenium自动化脚本,打包时执行的是 Python pyinstaller -F --onefile -w  XXX.py 这样打出的包执行后提示“failed to excute s ...

  5. python 序列类型

    1.不可变的序列类型:tuple.range.str.set 001:对于tuple 类型有如下几种构造方式 1.() 构造一个空的元组. 2.a | (a,) 构造一个只有一个元素的元组. 3.tu ...

  6. Data Mining 概念

    数据挖掘概念: 数据挖掘是在大型数据库中.自动的发现有用信息的过程. 然. 这个有用只是一个感性的东西.比如我们从表中索引一行数据.这个算不上数据挖掘.因为它依赖的是数据的明显特征. 数据挖掘基本步骤 ...

  7. 复习完毕STM32开发板

        经过半个晚上的折腾,终于复习了STM32开发板,并使用ST官方库调试完毕一个printf重定向到串口的程序,3.5的库同以前的库不大一样,不过最终搞好了可以睡觉了,还可以睡7个小时.     ...

  8. LoadRunner 技巧之THML 与 URL两种录制模式分析

    Loadrunner的Virtual User Generator 提供人脚本的录制功能,对于初学者来说,这大大的降低了编写脚本的门槛,loadrunner提供两种录制脚本的方式:Html_based ...

  9. 并发(Concurrency)和并行(Parallelism)的区别

    最近在读<real world haskell>里关于并行的一章时,看到作者首先对并发(Concurrency)和并行(Parallelism)的区别进行了定义和解释.以前我对这个问题也是 ...

  10. redhat 6.3 64位安装中文输入法全过程记录

    首先,修改/etc/profile文件,在末尾增加两行: export LC_ALL="zh_CN.UTF-8" export LANG="zh_CN.UTF-8&quo ...