一個可隨意定位置的帶色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的配置信息,可以設置它的顯示時長,而且可以設置它的點擊事件等。

后續的大家自己去創新啦。

你們的點贊是我分享的動力,所以如果你開心,那就動動小手點個贊吧~~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM