直播网站源码Android自定义toast显示效果-创新互联
编码实现

**ToastUtils.java**
public class ToastUtils {
private static Toast mToast;
public static void showShortText(String text){
showText(text,Toast.LENGTH_SHORT);
}
public static void showLongText(String text){
showText(text,Toast.LENGTH_LONG);
}
public static void showText(String text,int duration){
if (TextUtils.isEmpty(text)){
return;
}
TextView tvText;
if (mToast == null){
mToast = Toast.makeText(MyApplication.getContext(), "", Toast.LENGTH_SHORT);
final View toastLayout = ((LayoutInflater) MyApplication.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.toast_layout, null);
tvText = toastLayout.findViewById(R.id.toast_text);
mToast.setView(toastLayout);
}else {
mToast.cancel();
mToast = Toast.makeText(MyApplication.getContext(), "", Toast.LENGTH_SHORT);
final View toastLayout = ((LayoutInflater) MyApplication.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.toast_layout, null);
tvText = toastLayout.findViewById(R.id.toast_text);
mToast.setView(toastLayout);
}
mToast.setDuration(duration);
tvText.setText(text);
mToast.show();
}
// 普通方法.....
public static void showToast(String msg, int duration) {
mToast = Toast.makeText(MyApplication.getContext(), "", duration);
mToast.setText(msg);
mToast.show();
}
}
```
**toast_layout.xml**
```clike
**bg_toast**
使用
ToastUtils.showShortText("1111");
总结
自定义背景效果还是比较简单的,有利于统一机型效果,不是很复杂。主要是从toast源码提供的api我们进行了相应的扩展,容易的实现是建立在对原理的理解与运用上的,多多注重基础,一起前进。
标题名称:直播网站源码Android自定义toast显示效果-创新互联
网址分享:http://www.jxjierui.cn/article/ccgjgs.html


咨询
建站咨询
