码迷,mamicode.com
首页 > 移动开发 > 详细

魅族手机遇到修改Toast样式BUG

时间:2016-04-29 17:27:30      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

因为测试是个魅族手机出现各种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

标签:

原文地址:http://blog.csdn.net/q837784522/article/details/51248391

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!