您的位置:首页 > 移动开发 > Android开发

android中如何设置Toast 的样式

2016-03-09 13:55 525 查看
android自带的Toast 往往样式都不是很好看,灰色的背景,看着很不舒服,下面就来写一个小的demo来改变一下Toast的样式

1.创建一个class继承Toast

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

public c
4000
lass 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();方法就可以了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: