开源代码——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. Java转换

    1.如何将字符串String转化为整数int  int i = Integer.parseInt(str);   int i = Integer.valueOf(my_str).intValue(); ...

  2. Fragment之三:根据屏幕尺寸加载不同的Fragment

    Fragment一个重要的作用在于根据屏幕的尺寸或者方向加载不同的布局. 未完待续

  3. Android listview 的应用

    ListView作为Android最常用但是却最难用的控件之一,有很多神奇的用法.我之前也有写过一个例子,稍微不那么简单了一点. [Android原生item的伸缩效果]:http://www.cnb ...

  4. Android 底部Dialog显示

    public void showComplainDialog() { ComplainDialog complain_dialog = new ComplainDialog(OrderDetialAc ...

  5. Java中加载配置文件的集中方式,以及利用ClassLoader加载文件 .

    我们往常进行文件的加载的时候 用到的都是  FileInputStream进行 文件的加载比如下面一个例子 : InputStream in=FileInputStream("1.prope ...

  6. php in_array 和 str_replace

    有这样的用法哦 $allow_state_array_invoc = array('store_invoice','invoiceno','invoicerec','invoiceing'); if ...

  7. Oracle EBS-SQL (MRP-4):检查例外信息查询_建议取消_采购申请.sql

    select msi.segment1                    编码 ,msi.description                  描述 ,mr.old_order_quantit ...

  8. 对DTU系统结构的重新思考

        从决定做DTU开始无时无刻不在对这个新的产品系统进行思考,从最开始的ucos多任务结果到QPC基 于事件回调的软件结果,再到现在准备结合两者使用QPC+freeRTOS的系统结构.     原 ...

  9. C#多显示器转换的两种方法——SetWindowPos,Screen

    原文 http://blog.csdn.net/hejialin666/article/details/6057551 实现多屏显示目的:一般情况下是一个电脑显示屏,外接一个电视显示屏.在电脑上显示的 ...

  10. tomcat优化-有改protocol 和 缓存 集群方案

    tomcat优化 在线上环境中我们是采用了tomcat作为Web服务器,它的处理性能直接关系到用户体验,在平时的工作和学习中,归纳出以下七种调优经验. 1. 服务器资源 服务器所能提供CPU.内存.硬 ...