当你的才华还撑不起你的梦想时,你只能一直前进!

一个可随意定位置的带色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>

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

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

  

这里默认显示在顶部。

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

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

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

posted @ 2016-07-27 19:12  南尘  阅读(2253)  评论(1编辑  收藏  举报

写不完的矫情,做不完的开源

点击进入我的GitHub页
南 尘
主 页
优美钢琴曲合集-南尘.mp3                    感谢您阅读我的博客,如果您现在工作、学习累了或者疲惫了,不妨聆听一下音乐,它能够减轻你的疲劳,还能够带给您一种舒适愉悦的心情。(样式取自博客园-欲泪成雪)