自定义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 ...
随机推荐
- MFC框架类、文档类、视图类相互访问的方法
1.获取应用程序指针 CMyApp* pApp=(CMyApp*)AfxGetApp(); 2.获取主框架指针 CWinApp 中的公有成员变量 m_pMainWnd 就是主框架的指针 CMainFr ...
- 【HAOI2007】理想的正方形
[问题描述] 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. [输入] 第一行为3个整数,分别表示a,b,n的值第二行至第a+1行每行 ...
- css expression explaination
http://blogs.msdn.com/b/ie/archive/2008/10/16/ending-expressions.aspx 据说已经被弃用的IE css写法,为了修复一些IE8及老版本 ...
- UITextField AutoComplete iOS输入框内文本自动完成
当你打开Safari的时候,输入网址,会有许多候选网址,点击后,自动填充到输入框,进入网页. 打开词典查单词的时候,输入前面部分字母,软件会给出符合的候选单词. 这样做的目的,是为了省去用户繁琐的输入 ...
- TCP/IP-UDP
We read the world wrong but say that it deceives us. "我们看错了世界,却说世界欺骗了我们" 参考资料:TCP/IP入门经典 ( ...
- Spring+SpringMVC+Mybatis+MAVEN+Eclipse+项目完整环境搭建
1.新建一个Maven项目,创建父项目. 2.创建子项目模块 3.创建javaWeb项目 4.创建后的项目目录结构 5.Maven文件配置 parent父项目pom.xml文件配置 <?xml ...
- Delphi-CompareText 函数
函数名称 CompareText 所在单元 System.SysUtils 函数原型 function CompareText(const S1, S2: string): Integer; 函数功能 ...
- sqlserver 进行MD5加密
官方定义函数: HashBytes ( '<algorithm>', { @input | 'input' } ) <algorithm>::= MD2 | MD4 | MD ...
- Angular2案例rebirth开源
Angular2案例rebirth开源 在过去的几年时间里,Angular1.x显然是非常成功的.但由于最初的架构设计和Web标准的快速发展,逐渐的显现出它的滞后和不适应.这些问题包括性能瓶颈.滞后于 ...
- Substrings
hdu1238:http://acm.hdu.edu.cn/showproblem.php?pid=1238 题意:给你n个串,求一个子串,这个子串在所有串中都出现,或者在逆串中出现.求最大的这个子串 ...