首页 > 移动平台 > 详细

Android自定义Toast

时间:2015-04-14 19:46:04      阅读:198      评论:0      收藏:0      [点我收藏+]

没什么技术难点,照着api做的。


这是效果图

技术分享



我们先对Toast自定义一个布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <ImageView 
        android:src="@drawable/toast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView 
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textSize="25sp"
        />

</LinearLayout>


下面是关键代码,我把它封装成了一个类。


public class MyToast extends Toast{

	public MyToast(Context context,String msg) {
		super(context);
	    LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);	
	    View layout=inflater.inflate(R.layout.toast, null);
	    TextView text=(TextView) layout.findViewById(R.id.text);
	    text.setText(msg);
	    setGravity(Gravity.CENTER_VERTICAL, 0, 0);
	    setDuration(LENGTH_SHORT);
	    setView(layout);
	}

}






Android自定义Toast

原文:http://blog.csdn.net/lxw_xiangyu/article/details/45046151

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!