下面的代码用于在一个TextView中显示两个String。
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
TextView tv = new TextView(mContext.getApplicationContext(), R.);
String id = null;
if (convertView == null) {
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
musiccursor.moveToPosition(position);
id += musiccursor.getString(music_column_index);
tv.setText(id));
} else
tv = (TextView) convertView;
return tv;
}
我想为两个字符串设置不同的样式。第一粗体文本样式
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);
第二。小型文本样式
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
musiccursor.moveToPosition(position);
id += musiccursor.getString(music_column_index);
有办法做到吗?
您可以通过Spannable
:执行此操作
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
musiccursor.moveToPosition(position);
final String title = musiccursor.getString(music_column_index);
id = musiccursor.getString(music_column_index);
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
musiccursor.moveToPosition(position);
id += musiccursor.getString(music_column_index);
final SpannableStringBuilder sb = new SpannableStringBuilder(id);
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD);
sb.setSpan(bss, 0, title.length, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
tv.setText(sb));
然后你的第一部分将是粗体,第二部分将是正常的。
是的,使用这些:
tv.setTypeface(null, Typeface.BOLD_ITALIC);
tv.setTypeface(null, Typeface.BOLD);
tv.setTypeface(null, Typeface.ITALIC);
tv.setTypeface(null, Typeface.NORMAL);
如果您有2个String
,我认为最简单的方法是创建2个单独的TextView
,并根据您的需要应用上面的代码。
更新
如果我创建两个单独的TextView,如何返回它?
同样,我能想到的最简单的方法是创建一个List<String> ids
并使用它
因此,使用:
ids.add(musiccursor.getString(music_column_index));
代替:
id += musiccursor.getString(music_column_index);