自定义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 ...
随机推荐
- 4位bcd数转换为2进制数
DATA SEGMENTBUF DW 1234HBUF1 DW ? ;2进制数放到buf1内存单元DATA ENDSCODE SEGMENTASSUME CS:CODE ...
- (转载)小课堂UI-有关配色的一个小技巧
- N3292系列资料之RTC介绍
N3292系列资料之RTC介绍 1 RTC特性 Ø 拥有时间计数器(秒,分,时)和日历计数器,用来计算时间 Ø 绝对定时功能(秒,分,时,日,月,年) Ø 相对定时功能 Ø 支持12小时/24小时模式 ...
- javascript 键值对
<script type="text/javascript"> var arr = new Array(); arr['cn'] = '中国'; arr['usa'] ...
- c语言Winpcap编程构造并接收解析arp包
/* 程序功能: 1.构造arp包,并发送.程序参数顺序:源IP.目的IP.mac地址.flag 2.获取网络中的ARP数据包,解析数据包的内容.程序参数:日志文件名 winpacp中文技术文档(基本 ...
- python的sorted相关
Python 字典排序 在python里,字典是内置的数据类型,是个无序的存储结构,每一个元素是key-value对: 有关key的解释: sorted(L,key=by_name)中的key即by_ ...
- bzoj2066: [Poi2004]Gra
Description 让我们考虑一个在m x 1 的板子上玩的游戏,板子被从1 到 m编号. 现在板子上有n 个棋子, 每个都严格占据板子上的一个格子. 没有一个棋子占据格子m. 每个单独的移动遵循 ...
- iOS文件系统的管理-b
NSFileManager 判断一个给定路劲是否为文件夹 [self.fileManagerfileExistsAtPath:isDirectory:]; 用于执行一般的文件系统操作 (reading ...
- STM32f103------ADC(DMA)
STM32F10x ADC 技术指标: 分辨率: 12位分辨率 LSB=Vref+ / 2^(12) 转换时间: 采样一次至少14个ADC时钟周期 ,而ADC最高时钟周期为14MHz 选用采样 ...
- C++ BigInteger 大整数类模板(转)
#include <deque> #include <vector> #include <iostream> #include <string> #in ...