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

几经折腾,发现这个东西还真是好用。不但可以给Toast置底色,还可以随意定义显示位置,而且还可以让你自己去自定义。

Demo代码已同步至:https://github.com/nanchen2251/CroutonDemo

上个简单的运行图

:

Crouton有三种底色:Alert(红色),Info(蓝色),Confirm(绿色),各种颜色可以通过Style指定。

由于这个开源库就是一个自定义View,所以不用去导成library,直接去github或者去我的github链接下载crouton包里面的类即可。

这是简单的包里面的内容:

我自己写这个Demo就相当简单了,我都不好意思发出来。

大家可以看看:

MainActivity.java

 package com.example.nanchen.croutondemo;

 import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout; import com.example.nanchen.croutondemo.crouton.Crouton;
import com.example.nanchen.croutondemo.crouton.Style; public class MainActivity extends AppCompatActivity { private LinearLayout layout; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); layout = (LinearLayout) findViewById(R.id.main_ll);
} public void topBtnClick(View view) {
Crouton.makeText(this,"显示顶部对话框", Style.INFO).show();
} public void otherBtnClick(View view) {
Crouton.makeText(this,"显示顶部对话框", Style.ALERT,layout).show();
}
}

然后是xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.nanchen.croutondemo.MainActivity"> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="topBtnClick"
android:text="显示顶部位置的提示框"/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="otherBtnClick"
android:text="显示其他位置的提示框"/> <LinearLayout
android:layout_marginTop="100dp"
android:id="@+id/main_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout> </LinearLayout>

然后运行就可以了。

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

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

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

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="Overdraw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f95063"
android:gravity="center_vertical"
android:orientation="horizontal"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:scaleType="center"/> <TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="这是提示"
android:textColor="#ffffff"/> </LinearLayout>

修改一下原来的xml文件

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.nanchen.croutondemo.MainActivity"> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="topBtnClick"
android:text="显示顶部位置的提示框"/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="otherBtnClick"
android:text="显示其他位置的提示框"/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myBtnClick"
android:text="显示自定义的提示框"/> <LinearLayout
android:layout_marginTop="100dp"
android:id="@+id/main_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout> </LinearLayout>

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

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

  

这里默认显示在顶部。

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

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

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

最新文章

  1. 帆软报表FineReport数据连接中游标问题解决方案汇总
  2. Codeforces Round #148 (Div. 2)
  3. python执行mysqldump命令
  4. Java [leetcode 10] Regular Expression Matching
  5. Linq- ExcuteQuery用法
  6. Python自动化运维之25、Ajax
  7. java中浮点数的比较(double, float)(转)
  8. 求n!末尾0的个数
  9. UIResponder学习
  10. 连连看的原生JS实现
  11. redis数据类型-散列类型
  12. AndroidStudio3更改包名失败
  13. switch 循环中的case理解
  14. norflash芯片内执行(XIP)
  15. Vue中循环的反人类设计
  16. 20175202 《Java程序设计》第三周学习总结
  17. something backup
  18. HDU 6336 Matrix from Arrays (杭电多校4E)
  19. 从MongoDB里面取得json格式的数据,然后存为本地的json文件,然后再从json读取变为dict
  20. linux存储管理之磁盘配额

热门文章

  1. Angular2入门系列教程3-多个组件,主从关系
  2. 事务日志已满,原因为“ACTIVE_TRANSACTION”
  3. 由Dapper QueryMultiple 返回数据的问题得出==》Dapper QueryMultiple并不会帮我们识别多个返回值的顺序
  4. React的使用与JSX的转换
  5. jQuery学习之路(6)- 简单的表格应用
  6. 【转】java通用URL接口地址调用方式GET和POST方式
  7. JVM类加载
  8. Zookeeper常用命令
  9. 个人也能申请微信小程序获得APPID和手机测试效果
  10. 【腾讯Bugly经验分享】程序员的成长离不开哪些软技能?