標籤:
因為測試是個魅族手機出現各種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();}
魅族手機遇到修改Toast樣式BUG