0

我有一个TextView在自定义GridView适配器中使用的自定义。自定义TextView使用自定义字体进行翻译。这在默认安装了语言环境的设备中效果很好。

但是,在未安装该语言的设备中,它显示出一种奇怪的行为。当应用程序第一次加载时,TextViews不显示自定义字体。但是,当我按下刷新按钮重新加载片段时,会TextViews显示自定义字体。

我不确定为什么会这样。

Adapters我使用 custom 的应用程序中的所有自定义都会发生这种情况TextView

非常基本的适配器:

public class CalendarWeekAdapter extends BaseAdapter{

    private String[] weekdays;
    Context mContext;
    private LayoutInflater mInflater;

       public CalendarWeekAdapter(Context context, int firstDay)
       {
              mContext=context;
              mInflater = LayoutInflater.from(context);
              weekdays = context.getResources().getStringArray(R.array.weekdays); 
       }
       public int getCount()
       {
              return weekdays.length;
       }
       public Object getItem(int position)
       {
              return position;
       }
       public long getItemId(int position)
       {
              return position;
       }
       public View getView(int position, View convertView, ViewGroup parent)
       {
              ViewHolder holder=null;
              if(convertView==null)
              {
                     convertView = mInflater.inflate(R.layout.calendar_week, parent,false);
                     holder = new ViewHolder();
                     holder.txtWeekdays=(CustomTextView)convertView.findViewById(R.id.weekdays);
                     if(position==0)
                     {                             
                           convertView.setTag(holder);
                     }
              }
              else
              {
                     holder = (ViewHolder) convertView.getTag();
              }


             holder.txtWeekdays.setText(weekdays[position]);


             return convertView;
       }

}
class ViewHolder
{        
          CustomTextView txtWeekdays;                 
}

基本自定义文本视图:

public class CustomTextView extends TextView {
        public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }

        public CustomTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }

        public CustomTextView(Context context) {
            super(context);
            init();
        }

        private void init() {
            if (!isInEditMode()) {
                setTypeface(Utils.getFont(getContext()));
            }
        }
}
4

1 回答 1

1

这可能不是答案,但我不明白这个 if 语句:

                 holder.txtWeekdays=(CustomTextView)convertView.findViewById(R.id.weekdays);
                 if(position==0)
                 {                             
                       convertView.setTag(holder);
                 }

为什么会在那里?你所有新膨胀的 convertViews 都应该有 holder 作为它们的标签。只需删除 convertView.setTag(holder) 周围的“if”:

          if(convertView==null)
          {
                 convertView = mInflater.inflate(R.layout.calendar_week, null,false);
                 holder = new ViewHolder();
                 holder.txtWeekdays=(CustomTextView)convertView.findViewById(R.id.weekdays);
                 convertView.setTag(holder);
          }
          ...

看看这是否会改善甚至解决您的情况。

于 2013-02-06T23:30:43.273 回答