android中如何设置Toast 的样式

android自带的Toast 往往样式都不是很好看,灰色的背景,看着很不舒服,下面就来写一个小的demo来改变一下Toast的样式

1.创建一个class继承Toast

2.定义两个方法来显示Toast

public class CustomToast extends Toast{

	public CustomToast(Context context) {
		super(context);
	}
	
	public static void showText(Context context, int id)
	{
		showText(context, context.getString(id));
	}
	
	public static void showText(Context context, String text)
	{
   }
}

在上面的showText方法里面来实现具体的业务逻辑

1.创建出Toast实例

Toast toast=new Toast(context);

2.获得布局填充器,填充布局

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		LinearLayout contentView = (LinearLayout) inflater.inflate(R.layout.toast_custom, null);

3.找到LinearLayout里面的TextView然后将String text设置进去就好了

TextView mTextView = (TextView) contentView.findViewById(R.id.toast_text);
		mTextView.setText(text);
		toast.setView(contentView);


4.调用Toast的show();方法就可以了



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值