国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

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

2020-04-11 11:52:44
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 永福县| 隆子县| 秭归县| 田东县| 新巴尔虎右旗| 游戏| 丹棱县| 鹤壁市| 鲁山县| 曲靖市| 石渠县| 乐平市| 扬中市| 磐石市| 中卫市| 盐池县| 巨鹿县| 玉林市| 绿春县| 栾川县| 武宣县| 资中县| 陆良县| 巴林左旗| 桂东县| 宜君县| 丹凤县| 汉川市| 西青区| 嘉禾县| 凤阳县| 渝北区| 长治市| 敦化市| 中方县| 北碚区| 关岭| 密云县| 简阳市| 靖安县| 博罗县|