2011-11-22 79 views
0

我想知道是否可以添加一些样式到列表视图中使用的字符串?我已经尝试使用转义码的HTML格式,我似乎得到的是代码显示,但实际上并没有做我想做的事情。我想让LISTVIEW中的字符串变粗体,然后换行不粗体,如果可能的话使用更小的字体。我目前有BOLD代码,但似乎也没有做任何事情,它不会给我任何格式错误。Android ListView字符串样式

ListView menuList = (ListView) findViewById(R.id.ListView_Menu); 
String[] items = 
{ 
    getResources().getString(R.string.menu_item_one), 
    getResources().getString(R.string.menu_item_two), 
    getResources().getString(R.string.menu_item_three), 
    getResources().getString(R.string.menu_item_four), 
    getResources().getString(R.string.menu_item_five), 
    getResources().getString(R.string.menu_method_six), 
}; 
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, 
    R.layout.menu_item, items); 
menuList.setAdapter(adapt); 

琴弦这个样子

<string name="menu_item_one"><b>This should be BOLD</b>\nSshould should not be be bold and smaller font if possible</string> 
<string name="menu_item_two"><b>This should be BOLD</b>\nSshould should not be be bold and smaller font if possible</string> 
<string name="menu_item_three"><b>This should be BOLD</b>\nSshould should not be be bold and smaller font if possible</string> 
<string name="menu_item_four"><b>This should be BOLD</b>\nSshould should not be be bold and smaller font if possible</string> 
<string name="menu_item_five"><b>This should be BOLD</b>\nSshould should not be be bold and smaller font if possible</string> 
<string name="menu_item_six"><b>This should be BOLD</b>\nSshould should not be be bold and smaller font if possible</string> 
+0

请在提出问题时不要放弃。这让他们更难阅读,并且不会更快地得到答案。这也是非常烦人的,并且意味着有人必须编辑您的帖子以将其删除。此外,请在发布它时检查问题下方显示的预览,以检查代码格式等内容。谢谢。 –

回答

1

尝试使用getResources().getText()代替,让你的其他变量是一个CharSequence[]ArrayAdapter<CharSequence>。这应该将XML声明中的格式保存到每个列表项中视图中设置文本的位置。

为清楚起见,我重写了代码示例以这种方式:

ListView menuList = (ListView) findViewById(R.id.ListView_Menu); 
CharSequence[] items = 
{ 
    getResources().getText(R.string.menu_item_one), 
    getResources().getText(R.string.menu_item_two), 
    getResources().getText(R.string.menu_item_three), 
    getResources().getText(R.string.menu_item_four), 
    getResources().getText(R.string.menu_item_five), 
    getResources().getText(R.string.menu_item_six), 
}; 
ArrayAdapter<CharSequence> adapt = new ArrayAdapter<CharSequence>(this, R.layout.menu_item, items); 
menuList.setAdapter(adapt); 

希望帮助!

+0

不确定CharSequence []在哪里。我希望有一点指导。 TIA – WmBurkert

+0

我已经用适当的修改重写了您的示例,并删除了我的其他建议,因为我测试了此代码并且它可以工作。 – Devunwired