开源代码——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. jquery 鼠标右键事件、左键单击事件判定

    $(function(){ $('a').mousedown(function(e){ alert(e.which) // 1 = 鼠标左键 left; 2 = 鼠标中键; 3 = 鼠标右键 retu ...

  2. thinkphp微信开发:安全模式消息加解密

    使用thinkphp官方的WeChat包,使用不同模式可以成功,但是安全模式就是不行,现将分析解决结果做下记录. TRight 分析问题: 解密微信服务器消息老是不成功,下载下微信公众平台官方给出的解 ...

  3. git安装及使用简介

    从源代码安装 有人觉得从源码安装 Git 更实用,因为你能得到最新的版本. 二进制安装程序倾向于有一些滞后,当然近几年 Git 已经成熟,这个差异不再显著. 如果你想从源码安装 Git,需要安装 Gi ...

  4. keepalived 安装和配置

    第一步:安装 yum -y install keepalived 第二步:配置 /etc/keepalived/keepalived.conf ! Configuration File for kee ...

  5. MDX基础

    第一章 看了本书的第一章,总体一个印象,废话真多.话不多说:整理书中知识点,实践出真理! 知识点:MDX语法:简单的函数介绍; 首先语法网上流传的很多,读者应该具备cube(多维数据集)的知识基础,我 ...

  6. MVC与WebForm最大的区别

    原文地址:http://www.cnblogs.com/birdshover/archive/2009/08/24/1552614.html 使用ASP.NET MVC框架,创建默认项目,第一直观感觉 ...

  7. phpadmin

    一晚上都在调试数据库,都要疯了,整理如下: 0.Apache服务器的443端口与VMware的冲突,所以要更改配置文件.设为440就可以(这个随意). 1.因为要远程访问,默认密码为空,所以首先给ro ...

  8. c++多线程崩溃错误1

    主线程中的子线程没有jion,导致主线程马上结束,子线程对象被释放掉,而子线程还在后台继续执行导致崩溃 int main() OBJ = classA() OBJ.START()//在start函数中 ...

  9. redi中删除所有的数据

    EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 *

  10. 解决Mac OS Adobe Flash Builder 4.7 java heap space 问题【转】

    1. 在Finder中打开Adobe Flash Builder 4.7的安装目录 2. 在Adobe Flash Builder 4.7.app上点击右键“Show Package contents ...