2011-11-22 31 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