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"?>
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/toast_bg"
    android:padding="30dp" >
 
            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" />
            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" />

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

更多相关文章

  1. Android常用控件之RatingBar的使用
  2. Android监听软键盘的显示和隐藏
  3. Android好轮子-封装很好的库
  4. Android进阶之路 - 软键盘顶起解决方案
  5. Android学习之RecyclerView
  6. Android(安卓)首页轮播图实现
  7. Android完美解决多次点击Toast一直提示不消失问题
  8. Android(安卓)给图片、文字、控件、布局添加阴影效果(悬浮效果)
  9. Android(安卓)之 Shape (圆角输入框)

随机推荐

  1. 阿里云ECS centos6.8下安装配置MySql5.7
  2. MySql版本问题sql_mode=only_full_group_
  3. Mysql误操作后利用binlog2sql快速回滚的
  4. Windows下mysql5.7.18安装配置教程
  5. mysql中格式化数字详解
  6. Mysql事务操作失败如何解决
  7. mysql报错:Deadlock found when trying to
  8. 阿里云Linux CentOS 7.2下自建MySQL的roo
  9. Mybatis特殊字符处理的详解
  10. MySql索引详细介绍及正确使用方法