1. 程式人生 > >ToastCustom【自定義顯示風格的Toast】

ToastCustom【自定義顯示風格的Toast】

結構圖 iyu chan .cn 崩潰 頂部 組織 繼承 guid

版權聲明:本文為博主原創文章,未經博主允許不得轉載。

前言

基於系統Toast的自定義顯示風格的Toast。

效果圖

技術分享

代碼分析

  • ToastCustom類基於系統Toast,不是繼承Toast,只是通過toast.setView(view)方法引用自定義的顯示風格布局文件,達到自定義顯示風格的目的。
  • 為了和Toast用法保持一致,ToastCustom類中也使用了makeText、show、setGravity、setText方法。方便在項目中直接替換Toast。
  • 下面分析下ToastCustom類中的setText()方法

  該方法用來修改顯示的文本,剛開始的時候,我直接使用了toast.setText()方法進行修改文本:

  public void setText(CharSequence s){
     toast.setText(s);
  }

  但是程序崩潰了。分析原因得知toast的setText方法是找到系統Toast的系統布局文件mNextView中的ID值為message的TextView控件,然後修改這個TextView控件的文本實現的。(下面是源碼)

    /**
     * Update the text in a Toast that was previously created using one of the makeText() methods.
     * 
@param s The new text for the Toast. */ public void setText(CharSequence s) { if (mNextView == null) { throw new RuntimeException("This Toast was not created with Toast.makeText()"); } TextView tv = (TextView) mNextView.findViewById(com.android.internal.R.id.message);
if (tv == null) { throw new RuntimeException("This Toast was not created with Toast.makeText()"); } tv.setText(s); }

但是在ToastCustom類中我們已經修改了toast的布局文件引用,所以直接使用toast.setText()方法的時候,肯定找不到ID值為message的TextView控件。正確的代碼如下:

    public void setText(CharSequence s){
        TextView tv = (TextView) toast.getView().findViewById(R.id.tv_toast);
        tv.setText(s);
    }

使用步驟

一、項目組織結構圖

技術分享

註意事項:

1、 導入類文件後需要change包名以及重新import R文件路徑

2、 Values目錄下的文件(strings.xml、dimens.xml、colors.xml等),如果項目中存在,則復制裏面的內容,不要整個覆蓋

二、導入步驟

將ToastCustom復制到項目中,並重新import R文件

技術分享
package com.why.project.toastcustom.views;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.why.project.toastcustom.R;


/**
 * Create By HaiyuKing
 * Used 自定義Toast顯示風格,基於系統Toast【可以控制顯示樣式、位置,不可以控制顯示時間、動畫,不可觸發】
 * 註意 Toast布局在源碼中的布局是采用LinearLayout
 */
public class ToastCustom {

    private static ToastCustom toastCustom;
    private Toast toast;

    public static ToastCustom makeText(Context context, CharSequence text, int duration){
        LayoutInflater inflate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflate.inflate(R.layout.toast_custom_view, null);
        TextView tv = (TextView)view.findViewById(R.id.tv_toast);
        tv.setText(text);
        if (toastCustom == null) {
            toastCustom = new ToastCustom();
        }
        toastCustom.toast = new Toast(context);
        toastCustom.toast.setView(view);
        toastCustom.toast.setDuration(duration);

        return toastCustom;
    }

    public static ToastCustom makeText(Context context, int resId, int duration){
        return ToastCustom.makeText(context,context.getResources().getString(resId),duration);
    }

    public void show(){
        toast.show();
    }

    /**
     * 1、gravity是輸入Toast需要顯示的位置,例如CENTER_VERTICAL(垂直居中)、CENTER_HORIZONTAL(水平居中)、TOP(頂部)等等。
     * 2、xOffset則是決定Toast在水平方向(x軸)的偏移量,偏移量單位為,大於0向右偏移,小於0向左偏移
     * 3、yOffset決定Toast在垂直方向(y軸)的偏移量,大於0向下偏移,小於0向上偏移,想設大值也沒關系,反正Toast不會跑出屏幕。*/
    public void setGravity(int gravity, int xOffset, int yOffset) {
        toast.setGravity(gravity, xOffset, yOffset);
    }

    public void setText(CharSequence s){
        TextView tv = (TextView) toast.getView().findViewById(R.id.tv_toast);
        tv.setText(s);
    }

    public void setText(int resId){
        TextView tv = (TextView) toast.getView().findViewById(R.id.tv_toast);
        tv.setText(resId);
    }
}
ToastCustom.java

將toast_custom_view.xml文件復制到項目中

技術分享
<?xml version="1.0" encoding="utf-8"?>
<!-- 自定義顯示風格的Toast的布局文件 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_custom_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/toast_custom_view_bg"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_toast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/toast_custom_text_paddingTB"
        android:paddingBottom="@dimen/toast_custom_text_paddingTB"
        android:paddingLeft="@dimen/toast_custom_text_paddingLR"
        android:paddingRight="@dimen/toast_custom_text_paddingLR"
        android:text=""
        android:textColor="@android:color/white"
        android:textSize="@dimen/toast_custom_text_size"/>

</LinearLayout>
toast_custom_view.xml

將toast_custom_view_bg.xml文件復制到項目中

技術分享
<?xml version="1.0" encoding="utf-8"?>
<!-- 自定義顯示風格的Toast的布局文件的背景 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <!-- 圓角 -->
    <corners android:radius="@dimen/toast_custom_view_bg_corners" />
    <!-- 描邊 -->
    <stroke
        android:width="1dp"
        android:color="#666666" />
    <!-- 填充 -->
    <solid android:color="#666666" />

</shape>
toast_custom_view_bg.xml

在dimens.xml中添加以下顏色標記的代碼

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

    <!-- **************自定義顯示風格的Toast布局文件********************* -->
    <!-- 提示文字的大小 -->
    <dimen name="toast_custom_text_size">18sp</dimen>
    <!-- 提示文字的內邊距(上下) -->
    <dimen name="toast_custom_text_paddingTB">10dp</dimen>
    <!-- 提示文字的內邊距(左右) -->
    <dimen name="toast_custom_text_paddingLR">20dp</dimen>
    <!-- 背景的圓角 -->
    <dimen name="toast_custom_view_bg_corners">30dp</dimen>
</resources>

三、使用方法

ToastCustom toastCustom = ToastCustom.makeText(this,"自定義Toast顯示風格",Toast.LENGTH_LONG);
toastCustom.show();

如果想要修改文本或者顯示位置,參考下面的代碼:

ToastCustom toastCustom = ToastCustom.makeText(this,"自定義Toast顯示風格",Toast.LENGTH_LONG);
toastCustom.setText(R.string.app_name);
toastCustom.setGravity(Gravity.CENTER,0,0);
toastCustom.show();

混淆配置

參考資料

android 自定義Toast顯示風格

http://blog.csdn.net/yongxinzhenxi/article/details/25069415

Android:談一談安卓應用中的Toast情節(基礎)

http://www.cnblogs.com/net168/p/4041763.html

Android:剖析源碼,隨心所欲控制Toast顯示

http://www.cnblogs.com/net168/p/4058193.html

項目demo下載地址

鏈接:http://pan.baidu.com/s/1i4Q5pYH 密碼:8cya

ToastCustom【自定義顯示風格的Toast】