自定义Toast
简易自定义Toast
public class MainActivity extends ListActivity );//边角gradientDrawable.setGradientType(GradientDrawable.RECTANGLE);//矩形gradientDrawable.setColor(Color.YELLOW);//填充色gradientDrawable.setStroke(3, Color.WHITE);//描边mLayout.setBackground(gradientDrawable);mLayout.addView(tv);result.mNextView = mLayout;result.mDuration = duration;return result;}public static final int LENGTH_SHORT = 2000;public static final int LENGTH_LONG = 3500;private final Handler mHandler = new Handler();private int mDuration = LENGTH_SHORT;private int mGravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;private int mX, mY;private float mHorizontalMargin;private float mVerticalMargin;private View mView;private View mNextView;private WindowManager mWM;private final WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();public CToast(Context context) {init(context);}/*** Set the view to show.* @see #getView*/public void setView(View view) {mNextView = view;}/*** Return the view.* @see #setView*/public View getView() {return mNextView;}/*** Set how long to show the view for.* @see #LENGTH_SHORT* @see #LENGTH_LONG*/public void setDuration(int duration) {mDuration = duration;}/*** Return the duration.* @see #setDuration*/public int getDuration() {return mDuration;}/*** Set the margins of the view.** @param horizontalMargin The horizontal margin, in percentage of the* container width, between the container's edges and the* notification* @param verticalMargin The vertical margin, in percentage of the* container height, between the container's edges and the* notification*/public void setMargin(float horizontalMargin, float verticalMargin) {mHorizontalMargin = horizontalMargin;mVerticalMargin = verticalMargin;}/*** Return the horizontal margin.*/public float getHorizontalMargin() {return mHorizontalMargin;}/*** Return the vertical margin.*/public float getVerticalMargin() {return mVerticalMargin;}/*** Set the location at which the notification should appear on the screen.* @see android.view.Gravity* @see #getGravity*/public void setGravity(int gravity, int xOffset, int yOffset) {mGravity = gravity;mX = xOffset;mY = yOffset;}/*** Get the location at which the notification should appear on the screen.* @see android.view.Gravity* @see #getGravity*/public int getGravity() {return mGravity;}/*** Return the X offset in pixels to apply to the gravity's location.*/public int getXOffset() {return mX;}/*** Return the Y offset in pixels to apply to the gravity's location.*/public int getYOffset() {return mY;}/*** schedule handleShow into the right thread*/public void show() {mHandler.post(mShow);if (mDuration > 0) {mHandler.postDelayed(mHide, mDuration);}}/*** schedule handleHide into the right thread*/public void hide() {mHandler.post(mHide);}private final Runnable mShow = new Runnable() {public void run() {handleShow();}};private final Runnable mHide = new Runnable() {public void run() {handleHide();}};private void init(Context context) {final WindowManager.LayoutParams params = mParams;params.height = WindowManager.LayoutParams.WRAP_CONTENT;params.width = WindowManager.LayoutParams.WRAP_CONTENT;params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;params.format = PixelFormat.TRANSLUCENT;params.windowAnimations = android.R.style.Animation_Toast;params.type = WindowManager.LayoutParams.TYPE_TOAST;params.setTitle("Toast");mWM = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);}private void handleShow() {if (mView != mNextView) {// remove the old view if necessaryhandleHide();mView = mNextView;// mWM = WindowManagerImpl.getDefault();final int gravity = mGravity;mParams.gravity = gravity;if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) {mParams.horizontalWeight = 1.0f;}if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) {mParams.verticalWeight = 1.0f;}mParams.x = mX;mParams.y = mY;mParams.verticalMargin = mVerticalMargin;mParams.horizontalMargin = mHorizontalMargin;if (mView.getParent() != null) {mWM.removeView(mView);}mWM.addView(mView, mParams);}}private void handleHide() {if (mView != null) {if (mView.getParent() != null) {mWM.removeView(mView);}mView = null;}}}
自定义吐司布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/llToast"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#fff"android:orientation="vertical" ><TextViewandroid:id="@+id/tvTitleToast"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_margin="1dp"android:padding="1dip"android:background="#b000"android:gravity="center"android:textColor="#fff" /><LinearLayoutandroid:id="@+id/llToastContent"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="1dip"android:layout_marginLeft="1dip"android:layout_marginRight="1dip"android:background="#4000"android:orientation="vertical"android:padding="15dip" ><ImageViewandroid:id="@+id/tvImageToast"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center" /><TextViewandroid:id="@+id/tvTextToast"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:paddingLeft="10dip"android:paddingRight="10dip"android:textColor="#f000" /></LinearLayout></LinearLayout>
自定义Toast的更多相关文章
- Android带图片的Toast(自定义Toast)
使用Android默认的Toast Toast简介: Toast是一个简单的消息显示框,能够短暂的出现在屏幕的某个位置,显示提示消息. 默认的位置是屏幕的下方正中,一般Toast的使用如下: Toas ...
- 自定义Toast和RatingBar的简单用例
Toast是一个包含用户点击消息.Toast类会帮助你创建和显示这些.Android中的Toast是一种简易的消息提示框. 当视图显示给用户,在应用程序中显示为浮动 向右划动五角星增加 单击按钮显示自 ...
- 朝花夕拾-android 自定义toast
在一个只有你而且还未知的世界中,不去探索未知,死守一处,你到底在守什么呢? 作为一个目前的android程序员,可能过去写着delphi的代码,可能未来回去搭建服务器.不管怎样,你现在是一名安卓程序员 ...
- Android 自定义Toast
自定义Toast 其实就是自定义布局文件 感觉利用Dialog或者PopupWindow做也差不多 上图上代码 public class MainActivity extends Activity { ...
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
http://blog.csdn.net/wangfayinn/article/details/8065763 Android Toast用于在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失. ...
- Android开发必知--自定义Toast提示
开发过Android的童鞋都会遇到一个问题,就是在打印Toast提示时,如果短时间内触发多个提示,就会造成Toast不停的重复出现,直到被触发的Toast全部显示完为止.这虽然不是什么大毛病,但在用户 ...
- android 自定义Toast显示风格
1.创建一个自己想要显示Toast风格的XML如下代码(toast_xml.xml): <?xml version="1.0" encoding="utf-8&qu ...
- 提示框的优化之自定义Toast组件之(三)Toast组件优化
开发步骤: 在toast_customer.xml文件中添加一个图片组件对象显示提示图片 <?xml version="1.0" encoding="utf-8&q ...
- 提示框的优化之自定义Toast组件之(二)Toast组件的业务逻辑实现
在java下org.socrates.mydiary.activity下LoginActivity下自定义一个方法showCustomerToast() public class LoginAct ...
随机推荐
- 你好,C++(5)如何输出数据到屏幕、从屏幕输入数据与读写文件?
2.2 基本输入/输出流 听过HelloWorld.exe的自我介绍之后,大家已经知道了一个C++程序的任务就是描述数据和处理数据.这两大任务的对象都是数据,可现在的问题是,数据不可能无中生有地产生 ...
- javascript写的ajax请求
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...
- jquery ajax (1)原始js 实现
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Python学习笔记六--文件和输入输出
6.1文件对象 所有Python对文件的操作都是基于对文件对象的操作.那么就从文件对象的创建说起.open()[file()]提供初始化输入输出的接口.open()成功打开文件时会返回一个文件对象. ...
- D题(贪心)
D - D Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descripti ...
- 倒水问题-->经典面试题目
题目详细: 有两个容器,容积分别为A升和B升,有无限多的水,现在需要C升水.我们还有一个足够大的水缸,足够容纳C升水.起初它是空的,我们只能往水缸里倒入水,而不能倒出.可以进行的操作是:把一个容器灌满 ...
- 十大众筹PC:硅谷新生代如何打造下一代计算机
十大众筹PC:硅谷新生代如何打造下一代计算机 来源:CNET科技资讯网 众筹革命已经让众多吸引人的台式机,笔电和平板PC诞生.下面就是最引人注意和最成功的典范. 尽管PC市场不再象过去那样是一 ...
- -_-#【AJAX】XMLHttpRequest
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- CSDN总结的面试中的十大算法
1.String/Array/Matrix 在Java中,String是一个包含char数组和其它字段.方法的类.如果没有IDE自动完成代码,下面这个方法大家应该记住: toCharArray() / ...
- Android的JNI开发
变量的定义 int i; typedef int x;//定义一个int x数据类型 x a=10; printf("size=%d",sizeof(i));//获取int类型长度 ...