如何在Android中實(shí)現(xiàn)彩色Toast

本篇文章為大家展示了如何在Android中實(shí)現(xiàn)彩色Toast,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比通河網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式通河網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋通河地區(qū)。費(fèi)用合理售后完善,十多年實(shí)體公司更值得信賴。

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

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

Android是什么

Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。

上述內(nèi)容就是如何在Android中實(shí)現(xiàn)彩色Toast,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前題目:如何在Android中實(shí)現(xiàn)彩色Toast
網(wǎng)頁地址:http://muchs.cn/article18/jehddp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站排名、做網(wǎng)站、小程序開發(fā)、營銷型網(wǎng)站建設(shè)、

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)