最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 彩色Toast的實(shí)現(xiàn)代碼

 更新時(shí)間:2018年10月28日 09:18:54   作者:浮云Cloud  
這篇文章主要介紹了Android 彩色Toast的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

Android默認(rèn)的Toast太丑了,我們來(lái)封裝一個(gè)花里胡哨的Toast吧,就叫ColoredToast。

Github:https://github.com/imcloudfloating/DesignApp

效果:

Toast有一個(gè)setView方法,通過它我們可以設(shè)置自定義的布局,這里我只是加入了改變背景色,如果你有其它需求,比如加上圖標(biāo)也是可以的。

布局文件:一個(gè)FrameLayout和顯示消息的TextView

<?xml version="." encoding="utf-"?>
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">
   <TextView
     android:id="@+id/toast_message"
     android:layout_width="wrap_content"
     android:layout_height="dp"
     android:paddingStart="dp"
     android:paddingEnd="dp"
     android:gravity="center"
     android:textSize="sp"
     tools:text="This is a toast message" />
 </FrameLayout>

2.Java代碼:

用LayoutInflater來(lái)加載布局,然后用setView將布局設(shè)置為Toast的根View,通過自定義方法來(lái)設(shè)置Toast的消息和背景色,這里背景色是給TextView設(shè)置的,假如你想加上圖標(biāo)和其它元素,通過findViewById來(lái)設(shè)置即可。

這里我用的是GradientDrawable來(lái)作為Toast的背景,setColor方法背景色,setCornerRadius設(shè)置圓角半徑,最后將他作為TextView的背景就可以了。如果你不想用它,也可以直接使用xml文件來(lái)作為背景,不過這樣就不方便靈活設(shè)置顏色了。

 package com.cloud.customviews;
 import android.content.Context;
 import android.graphics.drawable.GradientDrawable;
 import android.support.annotation.ColorRes;
 import android.support.annotation.IntDef;
 import android.support.annotation.NonNull;
 import android.support.annotation.StringRes;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.TextView;
 import android.widget.Toast;
 public class ColoredToast extends Toast {
   @IntDef(value = {
       LENGTH_SHORT,
       LENGTH_LONG
   })
   @interface Duration {}
   private ColoredToast(Context context) {
     super(context);
   }
   public static class Maker {
     private Context mContext;
     private ColoredToast mToast;
     private View mToastView;
     private TextView mTextMessage;
     public Maker(Context context) {
       mContext = context;
       mToast = new ColoredToast(context);
       mToastView = LayoutInflater.from(context).inflate(R.layout.toast_colored, null);
       mTextMessage = mToastView.findViewById(R.id.toast_message);
     }
     /**
     * Set text color and background color for toast by resource id
     */
     public Maker setColor(@ColorRes int textColor, @ColorRes int backgroundColor) {
       GradientDrawable drawable = new GradientDrawable();
       drawable.setColor(mContext.getColor(backgroundColor));
       drawable.setCornerRadius(mTextMessage.getLayoutParams().height / );
       mToastView.setBackground(drawable);
       mTextMessage.setTextColor(mContext.getColor(textColor));
       return this;
     }
     /**
     * Set position
     * @see android.view.Gravity
     */
     public Maker setGravity(int gravity, int xOffset, int yOffset) {
       mToast.setGravity(gravity, xOffset, yOffset);
       return this;
     }
     public ColoredToast makeToast(@StringRes int resId, @Duration int duration) {
       mTextMessage.setText(resId);
       mToast.setView(mToastView);
       mToast.setDuration(duration);
       return mToast;
     }
     public ColoredToast makeToast(@NonNull String text, @Duration int duration) {
       mTextMessage.setText(text);
       mToast.setView(mToastView);
       mToast.setDuration(duration);
       return mToast;
     }
   }
 }

相關(guān)文章

  • Android 完全退出當(dāng)前應(yīng)用程序的四種方法

    Android 完全退出當(dāng)前應(yīng)用程序的四種方法

    Android程序有很多Activity,比如說(shuō)主窗口A,調(diào)用了子窗口B,如果在B中直接finish(), 接下里顯示的是A。在B中如何關(guān)閉整個(gè)Android應(yīng)用程序呢?本人總結(jié)了幾種比較簡(jiǎn)單的實(shí)現(xiàn)方法
    2016-02-02
  • Android中@id和@+id及@android:id的區(qū)別介紹

    Android中@id和@+id及@android:id的區(qū)別介紹

    這篇文章主要給大家介紹了關(guān)于Android中@id和@+id及@android:id的區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • android基于ListView和CheckBox實(shí)現(xiàn)多選和全選記錄的功能

    android基于ListView和CheckBox實(shí)現(xiàn)多選和全選記錄的功能

    本篇文章主要介紹了android基于ListView和CheckBox實(shí)現(xiàn)多選和全選記錄的功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2016-11-11
  • 微信小程序 跳轉(zhuǎn)頁(yè)面的兩種方法詳解

    微信小程序 跳轉(zhuǎn)頁(yè)面的兩種方法詳解

    這篇文章主要介紹了微信小程序 跳轉(zhuǎn)頁(yè)面的兩種方法詳解的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • Android利用MPAndroidChart繪制曲線圖表的基礎(chǔ)教程

    Android利用MPAndroidChart繪制曲線圖表的基礎(chǔ)教程

    最近在項(xiàng)目中要用到曲線圖,于是在網(wǎng)上找了很多很多,有AChartengine,MPAndroidChart,helloChart等等,我還用過基于html5的jsChart來(lái)做過,不過最終還是選擇了MPAndroidChart來(lái)做本文介紹了Android利用MPAndroidChart繪制曲線圖表的基礎(chǔ)教程,需要的朋友可以參考下。
    2018-03-03
  • 詳解Flutter中StatefulBuilder組件的使用

    詳解Flutter中StatefulBuilder組件的使用

    StatefulBuilder小部件可以在這些區(qū)域的狀態(tài)發(fā)生變化時(shí)僅重建某些小區(qū)域而無(wú)需付出太多努力。本文將來(lái)詳細(xì)講講它的使用,需要的可以參考一下
    2022-05-05
  • Android仿QQ好友列表實(shí)現(xiàn)列表收縮與展開

    Android仿QQ好友列表實(shí)現(xiàn)列表收縮與展開

    這篇文章主要介紹了Android仿QQ好友列表實(shí)現(xiàn)列表收縮與展開,感興趣的小伙伴們可以參考一下
    2015-12-12
  • Android讀取本地json文件的方法(解決顯示亂碼問題)

    Android讀取本地json文件的方法(解決顯示亂碼問題)

    這篇文章主要介紹了Android讀取本地json文件的方法,結(jié)合實(shí)例形式對(duì)比分析了解決顯示亂碼問題的方法,需要的朋友可以參考下
    2016-06-06
  • Android中各種Time API詳細(xì)

    Android中各種Time API詳細(xì)

    這篇文章要分享的是Android中各種Time API, SystemClock.uptimeMillis()、System.nanoTime(),下面我們就來(lái)看看他們到底有什么區(qū)別吧
    2021-10-10
  • flutter 微信聊天輸入框功能實(shí)現(xiàn)

    flutter 微信聊天輸入框功能實(shí)現(xiàn)

    這篇文章主要介紹了flutter 微信聊天輸入框功能實(shí)現(xiàn),本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03

最新評(píng)論

日照市| 安阳县| 潞城市| 江阴市| 察隅县| 土默特左旗| 无锡市| 邵阳县| 瓮安县| 疏勒县| 曲阳县| 英超| 乌拉特前旗| 托里县| 额济纳旗| 曲靖市| 巴南区| 梧州市| 金溪县| 灵璧县| 澄江县| 英山县| 类乌齐县| 庆城县| 比如县| 永丰县| 保定市| 神木县| 长兴县| 综艺| 兴山县| 缙云县| 丹凤县| 通河县| 师宗县| 股票| 浏阳市| 房产| 牡丹江市| 民县| 农安县|