android Toast 自定义样式

本文介绍了一种在Android应用中自定义Toast消息提示的方法,通过创建自定义布局和样式实现更美观的消息提示显示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  /**
  * 提示信息
  */
 private void displayToast(String msgStr)
 {
  Toast toast = new Toast(WorkForceActivity.this);
  LayoutInflater li = WorkForceActivity.this.getLayoutInflater();
  View view = li.inflate(R.layout.toast_style, null);      //此处引用定义的样式文件
  TextView txtTitle = (TextView) view.findViewById(R.id.txtTitle);
  txtTitle.setText(msgStr);
  TextView txtTime = (TextView) view.findViewById(R.id.txtTime);
  txtTime.setVisibility(View.GONE);
  toast.setView(view);
  toast.setDuration(50);
  toast.show();
 }

 

toast_style.xml  样式文件

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="300px"
    android:layout_height="100px"
    android:background="@layout/toast_color"  //引用toast_color.xml 文件
    >
    <TextView
    android:textColor="#666666"
     android:text="成功" android:id="@+id/txtTitle"
     android:layout_width="300px" android:layout_height="fill_parent" android:gravity="left"
     android:textSize="18px"
     ></TextView>
    <TextView
    android:textColor="#666666"
    android:textSize="18px"
     android:text="时间:2011-09-08 10:00" android:id="@+id/txtTime" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="left"></TextView>
</LinearLayout>

toast_color.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#D2F8FB"/>     
    <stroke android:width="2dp" android:color="#59D1F4" /> 
    <padding android:left="7dp" android:top="7dp" 
            android:right="7dp" android:bottom="7dp" /> 
    <corners android:radius="4dp" /> 
</shape>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值