自定义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 ...
随机推荐
- css margin collapse
css中存在margin collapse,即边界塌陷或边界重叠. http://www.w3cplus.com/css/understanding-bfc-and-margin-collapse.h ...
- php把excel数值格式转成日期格式问题
在excel中:40847对应2011-10-31,是日期的数值型表现形式. 在PHP中,echo date('Y-m-d H:i:s',40847);//结果1970-01-01 11:52:30 ...
- Memcached源码分析——process_command函数解析
以下为个人笔记 /** * process_command 在memcached中是用来处理用户发送的命令的, * 包括get set,add,delete,replace,stats,flush_a ...
- gets与scanf
gets与scanf 从功能上可以看出不同之处:1 终止条件不同: gets只有遇到\n时才会结束输入,而scanf遇到空格或制表符时,也会结束输入.比如输入"test string\n&q ...
- Swift互用性:采用Cocoa设计模式(Swift 2.0版)-b
本页包含内容: 委托(Delegation) 错误处理(Error Handling) 键值观察(Key-Value Observing) Target-Action模式(Target-Action) ...
- Using Swift with Cocoa and Objective-C(Swift 2.0版):开始--基础设置-备
这是一个正在研发的API或技术的概要文件,苹果公司提供这些信息主要是为了帮助你通过苹果产品使用这些技术或者编程接口而做好计划,该信息有可能会在未来发生改变,本文当中提到的软件应该以最终发布的操作系统测 ...
- ajax vs oauth
http://www.cnblogs.com/rush/archive/2012/05/15/2502264.html https://www.ibm.com/developerworks/cn/xm ...
- java.lang.NoClassDefFoundError: javax/servlet/ServletContext
方法1:把SpringBoot中main方法所在的class不再继承org.springframework.boot.context.web.SpringBootServletInitializer即 ...
- BestCoder Round #49
呵呵哒,1001的dfs返回值写错,wa了两发就没分了,1002显然是PAM可是我没学过啊!!!压位暴力可不可以...看看范围貌似不行,弃疗...1003根本不会做,1004想了想lcc发现不可做,那 ...
- VIJOS P1543极值问题
已知m.n为整数,且满足下列两个条件:① m.n∈1,2,…,K② (n^ 2-mn-m^2)^2=1编一程序,对给定K,求一组满足上述两个条件的m.n,并且使m^2+n^2的值最大.例如,若K=19 ...