Android自定义Toast弹出消息提示

        Android中时常要用到toast弹出消息提示,但有的时候,系统的提示样式不能满足设计者与开发者的要求,这时候就需要我们自己来自定义一个toast布局。

先上一段代码。

private  void showToast(String tishi) {
// TODO Auto-generated method stub
View layout = inflater.inflate(this,R.layout.toast, null);
TextView title = (TextView) layout.findViewById(R.id.toast_title);
title.setText(tishi);
Toast toast = new Toast(this);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setView(layout);
toast.show();
}

这段代码就是弹出toast消息。其中,inflater是private RelativeLayout inflater;this是Activity的上下文。R.layout.toast是自定义的toast布局。

R.layout.toast布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/toast_bg"
    android:padding="30dp" >
 
    <ImageView
        android:id="@+id/toast_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/app_name"
        android:src="@drawable/ic_launcher"
        android:visibility="gone" />
    <TextView
        android:id="@+id/toast_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/toast_icon"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:textColor="@color/controle_parental_text_color"
        android:textIsSelectable="false"
        android:textSize="28sp" />

</RelativeLayout>

在需要弹出提示的地方直接调用showToast方法,传入参数即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值