因为测试是个魅族手机出现各种bug记录下。
需求是这样
为了 和原生toast保持一致 不修改为自定义Toast 而是在原生上修改,按下面代码
public static void showMyToast(Activity context, String maney,String...content ){
Toast toast = Toast.makeText(context,content[0], Toast.LENGTH_SHORT);
LinearLayout toastView = (LinearLayout) toast.getView();
TextView tv = new TextView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity =Gravity.CENTER_HORIZONTAL;
View view = toastView.findViewById(android.R.id.message);
TextView tv_toast = null ;
ColorStateList color =null;
float tv_toast_size = 0;
if(view != null ){
tv_toast = (TextView) view;
color = tv_toast.getTextColors();
tv_toast_size = tv_toast.getTextSize();
tv.setTextColor(color);
}
TextView tv2 =null;
if (content.length>1) {
tv2 = new TextView(context);
toastView.addView(tv2,params);
tv2.setText(content[1]);
tv2.setTextColor(color);
tv2.setTextSize(TypedValue.COMPLEX_UNIT_PX,tv_toast_size);
}
tv.setText(maney);
Drawable drawable= UIUtils.getDrawable(R.drawable.ic_dish_opu_money);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
tv.setCompoundDrawables(drawable,null,null,null);
toastView.addView(tv, 0,params);
toast.show();
}
魅族手机居然是这样子显示
public static void showMyToast(Activity context, String maney, String...content ){
Toast toast = Toast.makeText(context,content[0], Toast.LENGTH_SHORT);
LinearLayout toastView = (LinearLayout) toast.getView();
TextView tv = new TextView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity =Gravity.CENTER_HORIZONTAL;
View view = toastView.findViewById(android.R.id.message);
TextView tv_toast = null ;
ColorStateList color =null;
Drawable bg_color =null;
float tv_toast_size = 0;
if(view != null ){
tv_toast = (TextView) view;
color = tv_toast.getTextColors();
tv_toast_size = tv_toast.getTextSize();
tv.setTextColor(color);
bg_color = tv_toast.getBackground();
//关键代码 因为魅族手机的是Toast的原有Textview背景色不为null toastlayout 背景色为null,而一般其他手机是相反的
if (bg_color!=null) {
toastView.setBackground(bg_color);
tv_toast.setBackground(null);
}
}
TextView tv2 =null;
if (content.length>1) {
tv2 = new TextView(context);
toastView.addView(tv2,params);
tv2.setText(content[1]);
tv2.setTextColor(color);
tv2.setTextSize(TypedValue.COMPLEX_UNIT_PX,tv_toast_size);
}
tv.setText(maney);
Drawable drawable= UIUtils.getDrawable(R.drawable.ic_dish_opu_money);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
tv.setCompoundDrawables(drawable,null,null,null);
toastView.addView(tv, 0,params);
toast.show();
}