android - Toast 了解及使用


1.Toast是android提供的一种提醒机制。它可以在不影响用户操作的情况下,提醒用户一些信息。比如在后台的Service,thread就能够通过toast提醒用户任务的完成状况。
2.我们由简入繁,一步步了解Toast的使用。


这是最常使用的方式,如下:


// Toast.makeText(context, text, duration).show();
// context   -   上下文
// text	     -   要显示的文本
// duration  -   显示持续的时间  Toast.LENGTH_SHORT   短时Toast.LENGTH_LONG    长时
 

效果如图:  

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  • 带图片的Toast,代码如下:
// 创建图片
	ImageView imageView = new ImageView(this);
	imageView.setImageResource(R.drawable.ic_launcher);
	// 获取toast实例
	Toast toast = Toast.makeText(this, "这是Toast", Toast.LENGTH_LONG);
	// 获取Toast的布局View - 获取到的是一个LinearLayout,详情请查看附录-1
	LinearLayout layoutView = (LinearLayout) toast.getView();
	//添加图片进布局View - 因为是LinearLayout 默认vertical 所以第二个参数是index
	layoutView.addView(imageView, 0);
	// 设置文本
	toast.setText("带图片的 toast");
	// 设置布局View
	toast.setView(layoutView);
	// 设置位置   Toast.setGravity(int gravity, int xOffset, int yOffset)
	toast.setGravity(Gravity.RIGHT, 20, -30);
	toast.setDuration(Toast.LENGTH_LONG);
	toast.show();


效果如图:



----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


  • 自定义的Toast,代码如下:

Toast toast = Toast.makeText(this, "这是Toast", Toast.LENGTH_LONG);
//直接Inflater 布局文件 - setView 就可以了
View view = LayoutInflater.from(this).inflate(R.layout.layoutview, null);
// 设置布局Viewtoast.setView(view);
toast.setDuration(Toast.LENGTH_LONG);toast.show();


 

效果如图:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

  • 附录 -   1 
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;
    }


----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="?android:attr/toastFrameBackground">

    <TextView
        android:id="@android:id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center_horizontal"
        android:textAppearance="@style/TextAppearance.Toast"
        android:textColor="@color/bright_foreground_dark"
        android:shadowColor="#BB000000"
        android:shadowRadius="2.75"
        />

</LinearLayout>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值