您的位置:首页 > 移动开发 > Android开发

自定义Toast组件

2016-10-27 11:38 393 查看

自定义Toast组件

本篇博客为原创博客,转载请复制 http://blog.csdn.net/u013298947/article/details/52944397连接哦~:

1.自定义Toast布局文件

自定义的Toast布局文件中包括ImageView和TextView,不用多解释:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="@+id/toast_lay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/feed_toast_bg">
<ImageView
android:id="@+id/is_feed_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:src="@drawable/feed_submit_ok"/>
<TextView
android:id="@+id/feed_submit_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/is_feed_ok"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="@string/feed_is_ok"
android:textColor="@color/white"
android:textSize="14sp"/>
</RelativeLayout>
</RelativeLayout>


2.代码中加载布局文件,进行Toast显示设置

代码块语法遵循标准markdown代码,例如:

public void showToast(Context mContext,String str) {
LayoutInflater inflater = getLayoutInflater();
View layout= inflater.inflate(R.layout.feed_toast,  (ViewGroup) findViewById(R.id.toast_lay));
ImageView image = (ImageView) layout.findViewById(R.id.is_feed_ok);
image1.setImageResource(R.drawable.feed_submit_error);
TextView feed_submit_txt = (TextView) layout.findViewById(R.id.feed_submit_txt);
feed_submit_txt.setText(str+"");
Toast toast = new Toast(mContext);
//设置Toast显示位置
toast.setGravity(Gravity.CENTER, 0, 0);
//设置Toast显示时间
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}


3.最后在代码中调用showToast方法即可进行自定义布局显示。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android