2013-09-21 86 views
0

我使用simple_list_item_single_choice作爲我的列表視圖項目。Android simple_list_item_single_choice以編程方式設置默認高度

下面是示例代碼

view = mInflater.inflate(android.R.layout.simple_list_item_single_choice, null); 
final CheckedTextView title = (CheckedTextView) view.findViewById(android.R.id.text1); 

這工作不錯,但我的列表視圖項目不漂亮的一面展示......我的意思是項目的身高恰好是內容的高度...... 我希望它像我使用simple_list_item1一樣填充,但我需要它與選擇無線電。

我該如何設置這個漂亮的最小高度?它應該是默認48 dip 試圖設置title.setMinimumHeight(48),但沒有運氣。

編輯:我知道這是可能的自定義佈局,但它正是我想要避免。

謝謝!

回答

0

請勿使用android.R.layout.simple_list_item_single_choice創建自定義佈局並使用。在自定義佈局中,您可以輕鬆設置高度和寬度。

+0

是的,我知道,但爲什麼要創建自定義佈局,如果我只想顯示與選擇列表...,single_choice是做什麼我只需要將物品放大一點就可以了 – Majky

0

我的答案是晚了,也許你解決這個問題......我解決了這個問題,如下所示:

int getListViewHeight(ListView list) { 
    ListAdapter adapter = list.getAdapter(); 

    list.measure(View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); 
    int listviewHeight = list.getMeasuredHeight() * adapter.getCount() + (adapter.getCount() * list.getDividerHeight()); 

    return listviewHeight; 
} 

用途:

ListView listView = (ListView) findViewById(R.id.yourlistviewId); 
listView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getListViewHeight(listView))); 

的列表視圖應該在LinearLayout中。如果在一個不同的佈局,必須修改LinearLayout.LayoutParams

具有良好的編碼:)

相關問題