Android自定义toast
来源:互联网 发布:手机淘宝怎么找相似 编辑:程序博客网 时间:2024/05/17 22:27
toast工具类
package com.travelsky.airportapp.utils;import android.content.Context;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.widget.TextView;import android.widget.Toast;import com.travelsky.airportapp.R;/** * Created by iwanglijun on 2016/10/11. */public class ToastBig { private static Toast toast; public static void toast(Context context, String chapterName) { LayoutInflater inflater = LayoutInflater.from(context); View view = inflater.inflate(R.layout.book_reading_seekbar_toast, null); TextView chapterNameTV = (TextView) view.findViewById(R.id.chapterName); chapterNameTV.setText(chapterName); toast = new Toast(context); //这一行是为了控制toast显示的位置 toast.setGravity(Gravity.CENTER_VERTICAL, 0, PixelFormat.formatDipToPx(context,70)); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(view); toast.show(); }}
Xml布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@null" > <RelativeLayout android:background="@drawable/book_reading_toast_bg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="24dp" android:layout_marginRight="24dp" android:gravity="center" android:orientation="vertical" android:paddingBottom="37dp" android:paddingTop="39dp" android:layout_gravity="center_vertical"> <TextView android:id="@+id/chapterName" android:layout_width="fill_parent" android:layout_height="wrap_content" android:ellipsize="end" android:gravity="center" android:singleLine="true" android:text="www.wanglijun.vip" android:textColor="#ffffff" android:textSize="20sp" /> </RelativeLayout></LinearLayout>
dip和px互换工具类
package com.travelsky.airportapp.utils;import android.app.Activity;import android.content.Context;import android.util.DisplayMetrics;/** * dip和px互换类 * Created by iwanglijun on 2016/10/11. */public class PixelFormat { /** * 把dip单位转成px单位 * * @param context context对象 * @param dip dip数值 * @return */ public static int formatDipToPx(Context context, int dip) { DisplayMetrics dm = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay() .getMetrics(dm); return (int) Math.ceil(dip * dm.density); } /** * 把px单位转成dip单位 * * @param context context对象 * @param px px数值 * @return */ public static int formatPxToDip(Context context, int px) { DisplayMetrics dm = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay() .getMetrics(dm); return (int) Math.ceil(((px * 160) / dm.densityDpi)); }}
自定义这个toast是因为系统默认的太小了,并且是在页面的下方,用pad使用的时候可能会注意不到,所以这个toast是个放大版的,故命名ToastBig
调用的时候,传入上下文和toast要显示的内容即可。
1 0
- android自定义Toast视图
- Android中自定义Toast
- Android自定义Toast样式
- android Toast 自定义样式
- 自定义Toast - Android代码
- android中自定义toast
- Android 自定义Toast
- android自定义Toast视图
- android 自定义toast
- 自定义Android Toast
- android自定义toast
- Android Toast自定义
- android自定义toast
- Android之自定义Toast
- Android 自定义Toast实现
- android 自定义toast
- Android Toast自定义
- android 自定义toast
- Spring amqp rabbmit入门详解
- Jenkins 项目配置里缺少了 触发远程构建 (例如,使用脚本) Trigger builds remotely 选项的解决办法
- Hello Programmer
- Java 正则表达式
- [LeetCode]--217. Contains Duplicate
- Android自定义toast
- Arcengine IElement 的节点编辑
- ie浏览器透明背景设置
- SQL中 IFnull 和 if函数
- 输入一个链表,从尾到头打印链表每个节点的值。
- Java中调用Weka中的Apriori算法
- android抽象类AsyncTask<Params, Progress, Result>
- tomcat 使用https 和 IOS 请求https URL
- mfc dll 小记