最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android自定義toast(widget開(kāi)發(fā))示例

 更新時(shí)間:2014年03月03日 15:46:22   作者:  
這篇文章主要介紹了android自定義toast(widget開(kāi)發(fā))示例,需要的朋友可以參考下

1、Toast控件:

通過(guò)查看源代碼,發(fā)現(xiàn)Toast里面實(shí)現(xiàn)的原理是通過(guò)服務(wù)Context.LAYOUT_INFLATER_SERVICE獲取一個(gè)LayoutInflater布局管理器,從而獲取一個(gè)View對(duì)象(TextView),設(shè)置內(nèi)容將其顯示

復(fù)制代碼 代碼如下:

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;
    }

定義布局文件:

復(fù)制代碼 代碼如下:

<?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類(lèi):

復(fù)制代碼 代碼如下:

public class MyToast {

    /**
     * 顯示自定義的土司
     * @param context 上下文
     * @param iconid 圖標(biāo)的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();
    }
}

相關(guān)文章

最新評(píng)論

苗栗市| 昭平县| 五指山市| 井陉县| 新疆| 荆门市| 伊川县| 乡宁县| 嘉祥县| 敖汉旗| 五家渠市| 大新县| 嘉鱼县| 浑源县| 申扎县| 灌云县| 和政县| 大同市| 遂溪县| 顺平县| 涪陵区| 平凉市| 襄垣县| 麟游县| 南陵县| 来凤县| 舒兰市| 孟连| 靖边县| 濮阳市| 天镇县| 万载县| 达孜县| 临安市| 永福县| 涿州市| 会昌县| 元谋县| 博客| 曲阜市| 恭城|