• 开源代码——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的配置信息,可以设置它的显示时长,而且可以设置它的点击事件等。

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

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

     
    分类: android
  • 相关阅读:
    没有纳入spring管理的类如何注入spring管理的对象
    Servlet里面url-pattern的通配符*的使用规则
    【转】request和response的页面跳转传参
    Mybatis-Generator逆向生成Po,Mapper,XMLMAPPER(一)
    Sae上传war包找不到编译文件
    Maven配置中scope说明
    Spring配置中的classpath和classpath*的区别
    国内的ntp服务器
    U盘拷贝文件大小有限制吗?
    Genymotion安装教程
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/5713342.html
Copyright © 2020-2023  润新知