自定义顶部弹出Toast
来源:互联网 发布:dombera音乐软件 编辑:程序博客网 时间:2024/06/11 11:19
package com.kye.base.utils;import android.content.Context;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.WindowManager;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;import com.kye.base.R;import java.lang.reflect.Field;/** * * 自定义Toast */public class CusttomToast extends Toast { private static Toast mToast; public CusttomToast(Context context) { super(context); } public static Toast makeText(Context context, CharSequence text, int duration) { if (mToast == null) { mToast = new Toast(context); } else { mToast.cancel(); mToast = new Toast(context); } //获取LayoutInflater对象 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); //获得屏幕的宽度 int width = wm.getDefaultDisplay().getWidth(); //由layout文件创建一个View对象 View view = inflater.inflate(R.layout.layout_top_toast, null); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT); TextView toastTextView = (TextView) view.findViewById(R.id.tv_toast); //设置TextView的宽度为 屏幕宽度 toastTextView.setLayoutParams(layoutParams); toastTextView.setText(text); mToast.setView(view); mToast.setGravity(Gravity.TOP, 0, 0); mToast.setDuration(duration); try { Object mTN = null; 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; params.windowAnimations = R.style.Animtop; } } } catch (Exception e) { LogOut.printStackTrace(e); } return mToast; } /** * 反射字段 * * @param object 要反射的对象 * @param fieldName 要反射的字段名称 * @return * @throws NoSuchFieldException * @throws IllegalAccessException */ 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; }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff6666" android:gravity="center_vertical" android:orientation="vertical"> <TextView android:id="@+id/tv_toast" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:maxHeight="@dimen/h300" android:minHeight="@dimen/h150" android:textColor="@android:color/white" android:textSize="@dimen/w50" /></LinearLayout>
阅读全文
0 0
- 自定义顶部弹出Toast
- android 自定义Toast显示风格顶部弹出自定义时间
- Android自定义弹出的Toast
- Android toast弹出时间自定义
- Android自定义Toast弹出消息提示
- 快速弹出窗口的(二):自定义toast
- 自定义toast外形,多次点击不会总是弹出toast
- Android自定义Toast 解决关闭通知 Toast无法弹出
- 自定义Toast,从顶部掉下,然后再弹一下
- Toast大全(默认,顶部,底部,各种自定义) Android
- Windows Phone 自定义弹出框和 Toast 通知
- Android 自定义屏幕中间Toast的不延时弹出
- Android中自定义toast工具,解决多次弹出toast后显示时间过长
- 自定义Toast
- 自定义Toast
- 自定义Toast
- 自定义Toast
- 自定义toast
- 多线程编程
- css 自定义属性(@apply var)
- webStorm一些使用小诀窍
- 网络编程
- Python学习笔记(1)字符串&列表&条件
- 自定义顶部弹出Toast
- 1060. 爱丁顿数(25)
- 超好用的热部署工具jrebel和eclipse的结合使用
- C++类实现字符串除去空格和排序
- osgearth 编译问题
- int main(int argc, char* argv[])用法解析
- 信源编码作业三
- 引用计数原则
- iOS之UILabel详解