亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

android 自定義Toast

標簽:
Android

Toast是android的一个简易消息提示框。

         它不会获得焦点,也无法被点击。向用户提示信息,却不停留着不动。

        其实,自定义Toast非常简单:

         先看效果:

                5be45b1100015b1703020135.jpg

          首先:新建一个mtoast.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@null"
    android:gravity="center_vertical"
    android:orientation="horizontal" >
    <RelativeLayout
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="34dp" >
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@drawable/display_bg_2" />
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:textColor="@android:color/white"
            android:text="TextView" />
    </RelativeLayout>
</LinearLayout>

在定义一个mTools.java文件:

    public class mTools extends Toast{
    private static Toast resToast;
public mTools(Context context) {
super(context);
}
public static void  showToast(Context context,int resId,CharSequence text,int duration){
//获取系统的LayoutInflater
LayoutInflater inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//加载布局文件
View view=inflater.inflate(R.layout.mtoast, null);
//实例化控件
ImageView imageView=(ImageView) view.findViewById(R.id.imageView1);
TextView textView=(TextView) view.findViewById(R.id.textView1);
//设置控件要显示的东西
imageView.setImageResource(resId);
textView.setText(text);
//实例化Toast
resToast=new Toast(context);
resToast.setView(view);     //设置要显示的view
resToast.setGravity(Gravity.CENTER, 0,0); //设置在屏幕的什么位置显示
resToast.setDuration(duration); //显示的时间长短
resToast.show();  //show
}
}

那么你只需要在Activity中

           mTools.showToast(MainActivity.this,R.drawable.display_bg_2,"测试Toast", Toast.LENGTH_SHORT);

就OK了,一个自定义的Toast就出来了!!!!!!!!

原文链接:http://www.apkbus.com/blog-524160-59269.html

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 1
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消