zoukankan      html  css  js  c++  java
  • Android学习笔记_54_自定义 Widget (Toast)

      1、Toast控件:

      通过查看源代码,发现Toast里面实现的原理是通过服务Context.LAYOUT_INFLATER_SERVICE获取一个LayoutInflater布局管理器,从而获取一个View对象(TextView),设置内容将其显示.

    public static Toast makeText(Context context, CharSequence text, int duration) {
            Toast result = new Toast(context);
    
            LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
            TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
            tv.setText(text);
            
            result.mNextView = v;
            result.mDuration = duration;
    
            return result;
        }

      定义布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    
        <ImageView
            android:id="@+id/iv_my_toast"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/notification" />
    
        <TextView
            android:id="@+id/tv_my_toast"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:textSize="18sp"
            android:text="text"
            />
    
    </LinearLayout>

      自定义MyToast类:

    public class MyToast {
    
        /**
         * 显示自定义的土司
         * @param context 上下文
         * @param iconid 图标的id
         * @param text 显示的文本
         */
        public static void showToast(Context context,int iconid, String text){
            View view = View.inflate(context, R.layout.my_toast, null);
               TextView tv = (TextView) view.findViewById(R.id.tv_my_toast);
            ImageView iv = (ImageView) view.findViewById(R.id.iv_my_toast);
            iv.setImageResource(iconid);
            tv.setText(text);
            Toast toast = new Toast(context);
            toast.setDuration(0);
            toast.setView(view);
            toast.show();
        }
        
    }
  • 相关阅读:
    CSS——如何清除浮动
    CSS——display(Block none inline等)属性的用法
    css3——position定位详解
    [转载]mysql创建临时表,将查询结果插入已有表中
    [转载]基于LVS的AAA负载均衡架构实践
    Percona Toolkit 2.2.19 is now available
    [转载]使用awk进行数字计算,保留指定位小数
    [转载]github在线更改mysql表结构工具gh-ost
    [转载]binlog归档
    [转载]MySQL运行状态show status详解
  • 原文地址:https://www.cnblogs.com/lbangel/p/3578334.html
Copyright © 2011-2022 走看看