android通過(guò)自定義toast實(shí)現(xiàn)懸浮通知效果的示例代碼
android通過(guò)toast實(shí)現(xiàn)懸浮通知效果,如圖:

實(shí)現(xiàn)的功能:
- 自定義懸浮彈窗;
- 點(diǎn)擊其他地方該布局不受影響;
- 可自定義顯示時(shí)間;
- 可以設(shè)置點(diǎn)擊事件;
代碼如下:
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import cn.droidlover.xdroidmvp.router.Router;
import io.slife.wallet.R;
import io.slife.wallet.config.IntentKey;
import io.slife.wallet.ui.NewsFlashDetailActivity;
public class PushToast {
private AppCompatActivity mActivity;
private static PushToast mInstance;
private Toast mToast;
private final int SHOW = 1;
private final int HIDE = 0;
private Object mTN;
private Method mShow;
private Method mHide;
private Field mViewFeild;
private long durationTime = 5*1000;
public static PushToast getInstance() {
if (mInstance == null) {
mInstance = new PushToast();
}
return mInstance;
}
public void init(AppCompatActivity activity) {
mActivity = activity;
}
public void createToast(String title, String content, Map<String,String> params) {
if (mActivity == null) {
return;
}
LayoutInflater inflater = mActivity.getLayoutInflater();//調(diào)用Activity的getLayoutInflater()
// LayoutInflater inflater = (LayoutInflater) context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.view_push_toast, null); //加載layout下的布局
LinearLayout llPushContent = (LinearLayout) view.findViewById(R.id.ll_push_content);
TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
TextView tvContent = (TextView) view.findViewById(R.id.tv_content);
tvTitle.setText(title);
tvContent.setText(content);
mToast = new Toast(mActivity);
mToast.setView(view);
mToast.setDuration(Toast.LENGTH_LONG);
mToast.setGravity(Gravity.TOP, 0, 0);
reflectEnableClick();
reflectToast();
llPushContent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newsFlashId = params.get("InformationID");
Router.newIntent(mActivity).to(NewsFlashDetailActivity.class).putString(IntentKey.NEWS_FLASH_ID,newsFlashId).launch();
handler.sendEmptyMessage(HIDE);
}
});
if(mShow != null && mHide != null){
handler.sendEmptyMessage(SHOW);
}else{
mToast.show();
}
}
private void reflectEnableClick() {
try {
Object mTN;
mTN = getField(mToast, "mTN");
if (mTN != null) {
Object mParams = getField(mTN, "mParams");
if (mParams != null
&& mParams instanceof WindowManager.LayoutParams) {
WindowManager.LayoutParams params = (WindowManager.LayoutParams) mParams;
//顯示與隱藏動(dòng)畫(huà)
// params.windowAnimations = R.style.ClickToast;
//Toast可點(diǎn)擊
params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//設(shè)置viewgroup寬高
params.width = WindowManager.LayoutParams.MATCH_PARENT; //設(shè)置Toast寬度為屏幕寬度
params.height = WindowManager.LayoutParams.WRAP_CONTENT; //設(shè)置高度
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 反射字段
*
* @param object 要反射的對(duì)象
* @param fieldName 要反射的字段名稱
*/
private static Object getField(Object object, String fieldName)
throws NoSuchFieldException, IllegalAccessException {
Field field = object.getClass().getDeclaredField(fieldName);
if (field != null) {
field.setAccessible(true);
return field.get(object);
}
return null;
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case SHOW:
handler.sendEmptyMessageDelayed(HIDE, durationTime);
show();
break;
case HIDE:
hide();
break;
}
}
};
public void reflectToast() {
Field field = null;
try {
field = mToast.getClass().getDeclaredField("mTN");
field.setAccessible(true);
mTN = field.get(mToast);
mShow = mTN.getClass().getDeclaredMethod("show");
mHide = mTN.getClass().getDeclaredMethod("hide");
mViewFeild = mTN.getClass().getDeclaredField("mNextView");
mViewFeild.setAccessible(true);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
}
}
public void show() {
try {
//android4.0以上就要以下處理
if (Build.VERSION.SDK_INT > 14) {
Field mNextViewField = mTN.getClass().getDeclaredField("mNextView");
mNextViewField.setAccessible(true);
LayoutInflater inflate = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = mToast.getView();
mNextViewField.set(mTN, v);
Method method = mTN.getClass().getDeclaredMethod("show", null);
method.invoke(mTN, null);
}
mShow.invoke(mTN, null);
} catch (Exception e) {
e.printStackTrace();
}
}
private void hide() {
try {
mHide.invoke(mTN, null);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}catch (NullPointerException ex){
ex.printStackTrace();
}
}
}
xml布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll_push_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/bg_push_message" android:orientation="vertical"> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/black" android:text="標(biāo)題" android:maxLines="2" android:ellipsize="end" android:textStyle="bold" android:textSize="@dimen/text_size_14" /> <TextView android:id="@+id/tv_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/black_808080" android:textSize="@dimen/text_size_13" android:maxLines="3" android:ellipsize="end" android:layout_marginTop="@dimen/margin_large" android:text="1"/> </LinearLayout>
點(diǎn)九格式圖片:

使用方法:
activity中需要初始化一次:
PushToast.getInstance().init(this);
調(diào)用:
PushToast.getInstance().createToast(msg.title,msg.text,umengPushEntity.getExtraMap());
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)氣泡布局/彈窗效果 氣泡尖角方向及偏移量可控
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)氣泡布局/彈窗效果,可控制氣泡尖角方向及偏移量,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android RecyclerView使用GridLayoutManager間距設(shè)置的方法
本篇文章主要介紹了Android RecyclerView使用GridLayoutManager間距設(shè)置的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
詳解Android WebView的input上傳照片的兼容問(wèn)題
本篇文章主要介紹了詳解Android WebView的input上傳照片的兼容問(wèn)題,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-08-08
Android連接服務(wù)器端的Socket的實(shí)例代碼
這篇文章主要介紹了Android連接服務(wù)器端的Socket的實(shí)例代碼,需要的朋友可以參考下2017-05-05
Android開(kāi)發(fā)之時(shí)間日期操作實(shí)例
這篇文章主要介紹了Android開(kāi)發(fā)之時(shí)間日期操作,是Android程序開(kāi)發(fā)中常見(jiàn)的一個(gè)功能,需要的朋友可以參考下2014-08-08
Android實(shí)戰(zhàn)APP啟動(dòng)速度優(yōu)化
本篇文章給大家通過(guò)實(shí)戰(zhàn)總結(jié)了Android開(kāi)發(fā)APP啟動(dòng)速度優(yōu)化的方法以及需要注意的地方,有需要的朋友可以參考下。2018-05-05
Android開(kāi)發(fā)之Picasso通過(guò)URL獲取用戶頭像的圓形顯示
這篇文章主要介紹了android開(kāi)發(fā)之Picasso通過(guò)URL獲取用戶頭像的圓形顯示,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-06-06

