2012-06-14 100 views
0

我在Android中创建了3行的列表视图。如果我想在行上设置不同的字体/颜色,我该如何实现?到目前为止,我已经尝试过不同的东西而没有任何成功这是我最近的尝试,我试图设置getComment()为斜体,但老实说,我不知道我在做什么:D。请帮忙!android在列表视图中设置不同的字体/颜色

public String toString() 
{ 
return this.getAlias() + " " + this.dateFormat.format(this.getDate()) + "\n" +   (Html.fromHtml("<i>" + this.getComment() + "</i>" + "<br />")); 
} 
+0

尝试阅读本文件第一个http://developer.android.com/resources/tutorials/views/hello-listview.html –

+0

您可以尝试创建自定义ArrayAdapter或Baseadpter并将视图充填为行。 –

+0

你解决了你的问题吗?如果其中一个答案对您有帮助,请将其标记为已接受:) – Roy

回答

0

我假设你已经在xml布局文件中定义了你的行。你可以用一些简单的参数更改文本的颜色/风格在一个TextView:

<TextView 
    ... 
    android:textColor="#FFFF0000" 
    android:textStyle="italic" 
/> 
+0

“我想在行上设置不同的字体/颜色” - 未为每行设置颜色和样式SAME – jk1971

+0

您可以创建和ArrayAdapter(按建议由罗伊在他的答案)。在getView()方法中,您可以通过调用myTextView.setColor(..)/ myTextView.setTypeface(..)来设置颜色/样式。这些值可以从你的物品,或位置或任何你喜欢的东西中获得。 – jorgenfb

0

你想要做的是有一个持有textviews列表视图,当你设置你设置一个文本视图文本CharSequence对不对?用CharSequence,你可以添加跨度,你可以使粗体,下划线,斜体,彩色等等。我很确定它会保持它的风格,当你把它放在列表视图中,没有理由为什么它不应该,也是我认为当您使用一个字符串的风格消失,所以你可能要使用的CharSequence

http://developer.android.com/reference/android/text/Spannable.html

使用范围,你也可以动态地改变容貌不使用硬编码XML

1

你可以做到这一点ListViewAdapter。在项目中,我创建扩展一个ArrayAdapter一个新的类:

class SummaryListAdapter extends ArrayAdapter<DynformSummary> { 
    static final int mViewResourceId = R.layout.dynformlist_item; 
    final Context mContext; 

    public SummaryListAdapter(Context context, DynformSummaryList items) { 
     super(context, mViewResourceId, items); 
     mContext = context; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = convertView; 
     if (view == null) { 
      LayoutInflater inflater = LayoutInflater.from(mContext); 
      view = inflater.inflate(mViewResourceId, parent, false); 
     } 

     DynformSummary summary = getItem(position); 
     if (summary != null) { 
      TextView nameView = (TextView) view 
        .findViewById(R.id.dynformSummary_name); 
      TextView createdOnView = (TextView) view 
        .findViewById(R.id.dynformSummary_createdOn); 
      TextView itemSummaryView = (TextView) view 
        .findViewById(R.id.dynformSummary_itemSummary); 
      java.text.DateFormat df = DateFormat.getDateFormat(mContext); 

      String itemSummary = summary.getItemSummary(); 
      if (itemSummary == null || itemSummary.length() == 0) { 
       itemSummary = mContext 
         .getString(R.string.placeholder_empty); 
      } 

      nameView.setText(summary.getName()); 
      createdOnView.setText(df.format(summary.getCreatedOn())); 
      itemSummaryView.setText(itemSummary); 
     } 

     return view; 
    } 
} 

在你的情况,你可以为每个不同的字体或颜色分离的布局XML,或者您也可以在getView运行时编辑字体/颜色方法。

相关问题