自定义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 ...
随机推荐
- Java学习----对象的构造
public class Teacher { private String teacherName; private String gendar; // 构造方法:初始化属性值,没有返回类型 publ ...
- Yii框架AR对象数据转化为数组
demo函数作用:将AR对象数据转化为数组 局限:仅用于findAll的多维数组,find一维数组可以先转化为多维数组的一个元素在使用 function actionIndex() { $data = ...
- 手把手教你使用python复杂一点点的装饰器
#只要@deco后面跟括号,都要 先传装饰器参数,返回 再传目标待装饰函数,返回 传目标函数的参数 #这个参数可以是类 def deco(arg):#装饰器的函数在这里传 print('0',arg) ...
- 温故而知新 C++ 类型转换
C语言类型转换 在C语言里用到的类型转换方式,一般都是用强制类型转换,语法:(类型说明符)(表达式),例如: (float)a 把a转换为实型,(int)(x+y) 把x+y的结果转换为整型.C语言这 ...
- JMS集群部署问题 java.net.ConnectException: Connection refused; No available router to destination
1:本地spring配置如下 <!-- JndiTemplate --> <bean id="jndiTpl" class="org.springfra ...
- 使用Pull解析器生成XML文件和读取xml文件
有些时候,我们需要生成一个XML文件,生成XML文件的方法有很多,如:可以只使用一个StringBuilder组拼XML内容,然后把内容写入到文件中:或者使用DOM API生成XML文件,或者也可以使 ...
- ChatCell
import UIKit let chatCellHeight: CGFloat = 72 let chatCellInsetLeft = chatCellHeight + 8 class Chat ...
- 【HDOJ】5063 Operation the Sequence
#include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 100005 #defin ...
- Linux下的memset函数
函数原型 void *memset(void *s, int c, size_t n); 函数功能 将以s为首的存储空间前n字节空间全部替换为参数c指定的数据. 返回值 更新后的首地址s. 头文件 # ...
- 下载cppunit
cppunit的官方地址:http://sourceforge.net/projects/cppunit 方式一:下载打包好的版本 下载地址:http://sourceforge.net/projec ...