在android中在运行时设置ListView的高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11465833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Height of ListView at run time in android
提问by Sekar
How to set Height of ListView.
如何设置 ListView 的高度。
XML Code (This code is working fine and fixed height as 200 dip)
XML 代码(此代码工作正常,高度固定为 200 倾角)
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:orientation="vertical" >
<ListView
android:id="@+id/dialog_ListView"
android:layout_width="wrap_content"
android:layout_height="200dip"
android:padding="5dip"
android:layout_weight="1"
android:dividerHeight="5dip"
android:divider="@android:color/transparent" />
</LinearLayout>
Android Code (This code not fixed width and height)
Android 代码(此代码不固定宽度和高度)
convertView = mInflater.inflate( R.layout.dialog_page, null );
convertView.setLayoutParams( new ListView.LayoutParams( 200, 300) );
Android Code (This code i got Error Message)
Android 代码(此代码我收到错误消息)
convertView = mInflater.inflate( R.layout.lock_file_page, parent );
convertView.setLayoutParams( new ListView.LayoutParams( 200, 300) );
Error Message
错误信息
java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
How do fix Width and Height at Runtime.
如何在运行时修复宽度和高度。
Thanks in advance.
提前致谢。
回答by Mehul Santoki
Try This code..
试试这个代码..
LayoutParams lp = (LayoutParams) mListView.getLayoutParams();
lp.height = 300;
mListView.setLayoutParams(lp);
回答by Andres Marin
public static boolean setListViewHeightBasedOnItems(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter != null) {
int numberOfItems = listAdapter.getCount();
// Get total height of all items.
int totalItemsHeight = 0;
for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
View item = listAdapter.getView(itemPos, null, listView);
item.measure(0, 0);
totalItemsHeight += item.getMeasuredHeight();
}
// Get total height of all item dividers.
int totalDividersHeight = listView.getDividerHeight() *
(numberOfItems - 1);
// Set list height.
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalItemsHeight + totalDividersHeight;
listView.setLayoutParams(params);
listView.requestLayout();
return true;
} else {
return false;
}
}
回答by Dipak Keshariya
First Remove import files for LayoutParams and after that Use below code for LayoutParams.
首先删除 LayoutParams 的导入文件,然后使用下面的 LayoutParams 代码。
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(200, 300);
convertView.setLayoutParams(params);
回答by Zahra.HY
try this
尝试这个
public void setDynamicHeight(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
if(listItem != null){
listItem.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();}