Android開源:UIToast - 一個統一 APP Toast 樣式的庫

dlwdlw 7年前發布 | 10K 次閱讀 Android開發 移動開發 TOAST

UIToast

方便統一 Android APP Toast 風格

如何定制Toast全局樣式?

第一步:

values/colors.xml

<color name="colorToastTextColor">@android:color/white</color>
<color name="colorToastBackgroundColor">@color/colorPrimaryDark</color>

第二步:

values/styles.xml

<style name="AnimationToast">
        <item name="android:windowEnterAnimation">@anim/toast_fade_enter</item>
        <item name="android:windowExitAnimation">@anim/toast_fade_exit</item>
    </style>

第三步:

UIToast.show...

UIToast.showStyleToast(this,res.getString(R.string.theme_color));

Gradle

compile 'com.didikee:uitoast:0.1.0'

功能

1. 自定義 Toast 字體顏色

  1. 自定義 Toast 背景顏色
  2. 自定義 Toast 展示位置(類似 Popupwindow 在某某下方(正下方水平居中)展示)
  3. 自定義 Toast 進出場動畫</code></pre>

    代碼調用示例:

    1. 默認系統樣式:
    UIToast.showToast(this,res.getString(R.string.normal));
    1. 主題樣式 你得先在xml配置好樣式,包括顏色和動畫,如果你不配置的話那默認會調用此libray中的樣式
    UIToast.showStyleToast(this,res.getString(R.string.theme_color));
    1. 自定義Toast 顏色
    UIToast.showStyleToast(this,res.getString(R.string.custom_color),false, Color.GRAY,Color.GREEN, UIToast.NONE);
    1. 在屏幕中展示Toast(示例以屏幕左上角為參考系)
    UIToast.showLocationToast(this,res.getString(R.string.location_Screen),false,null, Gravity.START|Gravity.TOP,100,100);
    1. 在任意 View 正下方展示Toast(默認是在正下方,水平居中)
    UIToast.showLocationToast(this,res.getString(R.string.location_view),false,v, UIToast.NONE,0,0);
    1. 自定義動畫樣式
    UIToast.showBaseToast(this,res.getString(R.string.animation),true,UIToast.NONE,UIToast.NONE,null,UIToast.NONE,UIToast.NONE,UIToast.NONE,R.style.AnimationToast);

    關于 UIToast2 這個類的說明

    去看源碼的可以看到,方法重載什么的相對比較繁多,所以提供另一種寫法: 例如,展示一個自定義動畫,位置(帶偏移量),文字顏色,背景色的 Toast :

    DisplayMetrics displayMetrics2 = getResources().getDisplayMetrics();
                 int widthPixels2 = displayMetrics2.widthPixels;
                 int heightPixels2 = displayMetrics2.heightPixels;
                 new UIToast2.Builder(this)
                         .setText("UIToast2 with Builder")
                         .setTextColor(Color.WHITE)
                         .setBackgroundColor(res.getColor(R.color.colorPrimaryDark))
                         .setAnimations(R.style.AnimationToast)
                         .gravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL)
                         .offset(widthPixels2/4, (int) (heightPixels2*0.6))
                         .show();

     

     

 本文由用戶 dlwdlw 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!