自定义Toast组件

自定义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方法即可进行自定义布局显示。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值