开源代码——Crouton
开源代码——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的配置信息,可以设置它的显示时长,而且可以设置它的点击事件等。
后续的大家自己去创新啦。
你们的点赞是我分享的动力,所以如果你开心,那就动动小手点个赞吧~~
开源代码——Crouton的更多相关文章
- 一个可随意定位置的带色Toast——开源代码Crouton的简单使用
今天在公司要求的代码中,要求显示的提示能够更加具有多样化,而不是简单的Toast字样,第一想法肯定是自定义View呀,结果在浏览中发现还有这样的一个开源代码——Crouton. 几经折腾,发现这个东西 ...
- GitHub + VSTS 开源代码双向同步
GitHub已经是全球开源代码的大本营了,通过以下统计你可以看到仅仅javascript在github就有超过32万个活动的repo.很多开发人员都会把自己的一部分代码分享到github上进行开源,一 ...
- CWMP开源代码研究5——CWMP程序设计思想
声明:本文涉及的开源程序代码学习和研究,严禁用于商业目的. 如有任何问题,欢迎和我交流.(企鹅号:408797506) 本文介绍自己用过的ACS,其中包括开源版(提供下载包)和商业版(仅提供安装包下载 ...
- iOS流行的开源代码库
本文介绍一些流行的iOS的开源代码库 1.AFNetworking 更新频率高的轻量级的第三方网络库,基于NSURL和NSOperation,支持iOS和OSX.https://github.com/ ...
- CWMP开源代码研究2——easycwmp安装和学习
声明:本文是对开源程序代码学习和研究,严禁用于商业目的. 如有任何问题,欢迎和我交流.(企鹅号:408797506) 本文所有笔记和代码可以到csdn下载:http://download.csdn.n ...
- CWMP开源代码研究1——开篇之作
原创作品,转载请注明出处,严禁非法转载.如有错误,请留言! email:40879506@qq.com 声明:本系列涉及的开源程序代码学习和研究,严禁用于商业目的. 如有任何问题,欢迎和我交流.(企鹅 ...
- 开源代码Window下搭建rtmp流媒体服务器
合肥程序员群:49313181. 合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入) Q Q:408365330 E-Mail:egojit@qq.com 综合:有这样需求,将摄像头 ...
- AgileEAS.NET SOA 中间件平台 5.2 发布说明-包含Silverlight及报表系统的开源代码下载
一.AgileEAS.NET SOA 中间件简介 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速 ...
- 苹果刷机相关开源代码(如iRecovery等)收集汇总(不断更新中...)
下面截图是在下面开源代码下使用VS2015修改部分代码后适配而成,可以在Windows平台上运行, 下载连接: http://pan.baidu.com/s/1i4zKGx3.
随机推荐
- oracle存储参数(storage子句)含义及设置技巧
可用于:表空间.回滚段.表.索引.分区.快照.快照日志 参数名称 缺省值 最小值 最大值 说明 INITIAL 5(数据块) 2(数据块) 操作系统限定 分配给Segment的第一个Extent的大小 ...
- 修改ecshop模板体会
在上一篇中给大家带来了ecshop的总体的框架.从总体上看ecshop,相信大家的思路应该很清楚.作为一个对开源项目修改者你来说,能对ecshop有个初步的了解就行了,下面我会给大家带来我在修改ecs ...
- Python成长之路第二篇(3)_字典的置函数用法
字典的置函数用法(字典dict字典中的key不可以重复) class dict(object): """ dict() -> new empty dictionar ...
- 【测试环境】java|jdk|ant
很多文章都有写啊,我只是汇总一下:现在java已经是1.7+了,但是我们很多的时候开发环境还是在1.5.16左右,需要自己去配置: 0.为了方便切换测试环境,我们可以把jdk放到一个比较固定的位置.比 ...
- U盘开发之SCSI命令
借助硬件USB协议分析仪,可以清楚的看到U盘启动时和上位机之间交互的USB协议流程,从get desciptor get congfiguration set configuration到scsi命令 ...
- bzoj2014 [Usaco2010 Feb]Chocolate Buying
Description 贝西和其他奶牛们都喜欢巧克力,所以约翰准备买一些送给她们.奶牛巧克力专卖店里 有N种巧克力,每种巧克力的数量都是无限多的.每头奶牛只喜欢一种巧克力,调查显示, 有Ci头 ...
- OpenStack开启sshd
修改配置sshd的文件 1. 修改sshd配置文件 /etc/ssh/sshd_config 2. 将#PasswordAuthentication no的注释去掉,并将no改为y ...
- Z.Studio高级成衣定制(双井店)价格,地址(图)-北京-大众点评网
Z.Studio高级成衣定制(双井店)价格,地址(图)-北京-大众点评网 Z.Studio高级成衣定制(双井店)
- redhat enterprise 6.3 x86_64 上安装VirtualBox详细教程
这个教程真难找..... 安装第一步遇到的问题就是不能使用yum安装包,这是由于redhat是收费版,所以需要更新yum源列表,具体可以参考 http://www.cnblogs.com/tina-s ...
- Python学习笔记10-Python MysqlHelper ,MySql 辅助类
自己写了一个MySql辅助类,有需要的拿走: #--encoding:utf-8-- # import MySQLdb class MySQLHelper: myVersion=0.1 def __i ...