1. 程式人生 > >Android-自定義圓角Toast提示框

Android-自定義圓角Toast提示框

有時候我們根據需求需要自己指定toast的樣式,例如在介面中間顯示、圓角、帶圖片什麼的。

今天我給大家帶來的就是自定義圓角帶圖片顯示的toast提示框,先來看看最終效果圖:圖一為圓角帶圖片顯示的,圖二為圓角普通顯示。均為居中顯示

圖1:帶圖片的toast          圖2:不帶圖片的圓角toast

先來看看圓角的實現:顏色設為黑色404040,85%透明度B2

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#B2404040" />

    <corners
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp" />

</shape>

然後使用圓角設定作為RelativeLayout的背景,寫一個圖片在上,文字在下的佈局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/progress_hud_bg"
    android:paddingBottom="35dp"
    android:paddingLeft="30dp"
    android:paddingRight="30dp"
    android:paddingTop="35dp" >

    <ImageView
        android:id="@+id/ImageView"
        android:layout_width="37dp"
        android:layout_height="37dp"
        android:layout_centerHorizontal="true" />


    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ImageView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:textColor="#FFFFFF"
        android:textSize="18sp" />


</RelativeLayout>

自定義toast的實現程式碼:

package com.example.toasttest;

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

public class ToastUtil extends Toast {
	private static Toast mToast;

	public ToastUtil(Context context) {
		super(context);
	}

	/**
	 * 自定義Toast樣式
	 * 
	 * @description
	 * @param context
	 * @param resId
	 * @param text
	 * @param duration
	 *            hrq 2014-7-10下午2:15:36
	 */
	public static Toast makeText(Context context, int resId, CharSequence text,
			int duration) {
		Toast result = new Toast(context);

		// 獲取LayoutInflater物件
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// 由layout檔案建立一個View物件
		View layout = inflater.inflate(R.layout.toast_hud, null);

		// 例項化ImageView和TextView物件
		ImageView imageView = (ImageView) layout.findViewById(R.id.ImageView);
		TextView textView = (TextView) layout.findViewById(R.id.message);

		//這裡我為了給大家展示就使用這個方面既能顯示無圖也能顯示帶圖的toast
		if (resId == 0) {
			imageView.setVisibility(View.GONE);
		} else {
			imageView.setImageResource(resId);
		}
		
		textView.setText(text);

		result.setView(layout);
		result.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
		result.setDuration(duration);

		return result;
	}

	public static void showToast(Context context, int resId, String content) {

		mToast = ToastUtil.makeText(context, resId, content, 100);
		mToast.show();
	}

}

之後我們就能在任意Activity裡呼叫這個自定義的Toast了:
<pre name="code" class="java">
package com.example.toasttest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {
    private ImageView mIv;
    private boolean isShow = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mIv = (ImageView) findViewById(R.id.iv);
        mIv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.iv:
            if (isShow) {
                //在ToastUtil裡做了封裝,就可以直接使用,不用每次都手動定義顯示的時間間隔等等。
                ToastUtil.showToast(getApplicationContext(),
                        R.drawable.ic_launcher, "網路異常,請檢視網路");
                isShow = false;
            } else {
                ToastUtil.showToast(getApplicationContext(),
                        0, "網路異常");
                isShow = true;
            }
            break;

        default:
            break;
        }
    }

}
demo下載