一个可随意定位置的带色Toast——开源代码Crouton的简单使用

时间:2023-02-22 22:31:55
一个可随意定位置的带色Toast——开源代码Crouton的简单使用

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

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

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

上个简单的运行图

:一个可随意定位置的带色Toast——开源代码Crouton的简单使用

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

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

这是简单的包里面的内容:一个可随意定位置的带色Toast——开源代码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>

然后运行就可以了。

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

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

一个可随意定位置的带色Toast——开源代码Crouton的简单使用

实现很简单,我自己写了一个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的配置信息,可以设置它的显示时长,而且可以设置它的点击事件等。

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

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