更改所选列表项的字体样式

时间:2014-03-12 08:26:43

标签: android android-listview android-arrayadapter listitem

我有一个菜单抽屉,其中包含一个包含自定义ArrayAdapter的列表。我希望将所选列表项的字体样式更改为粗体,但我不确定如何。

根据我的研究,似乎我应该在自定义适配器的getView中执行此操作。这是我的getView,我该怎么做?

public View getView(int position, View convertView, ViewGroup parent) {
  return getItem(position).getView(mInflater, convertView);
}

编辑:尝试更改getView中的字体,如下所示,但无法正常工作。电视似乎总是空的。

public View getView(int position, View convertView, ViewGroup parent) {
  TextView tv;
  View myView
  myView = getItem(position).getView(mInflater, convertView);
  tv = (TextView) myView.findViewById(R.id.list_content1);
  if (tv != null)
    tv.setTypeface(null, Typeface.BOLD_ITALIC);
  return myView;
}

感谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
TextView tv;

   if (convertView == null)
        convertView = View.inflate(context, android.R.layout.YOURLAYOUT)
        TextView tv  = (TextView) convertView.findViewById(R.id.YOURTEXTVIEW);
        holder.tv = tv;
        convertView.setTag(holder);
    else{
        holder = (ViewHolder) convertView.getTag();
        tv = holder.tv;
    }

    //now - find your textview based on your conditions. Just an example.
    tv.setText(getItem(position)); 
    if (tv.getText().equals("changeme"){
        tv.setTypeface(null, Typeface.BOLD); // Typeface.NORMAL, Typeface.ITALIC etc.

  return convertView;
 }