如何在Android中动态更改ListView高度?
我需要在我的应用程序中动态更改ListView的高度如何在Android中动态更改ListView高度?,android,listview,Android,Listview,我需要在我的应用程序中动态更改ListView的高度 有什么方法可以做到这一点吗?要动态更改高度和宽度,请尝试以下方法 RelativeLayout.LayoutParams mParam = new RelativeLayout.LayoutParams((int)(width),(int)(height); listView.setLayoutParams(mParam); 您也可以使用LayoutParams.FILL\u父级或LayoutParams.WRAP\u内容而
有什么方法可以做到这一点吗?要动态更改高度和宽度,请尝试以下方法
RelativeLayout.LayoutParams mParam = new RelativeLayout.LayoutParams((int)(width),(int)(height);
listView.setLayoutParams(mParam);
您也可以使用LayoutParams.FILL\u父级或LayoutParams.WRAP\u内容而不是高度和宽度使用自定义的listview并将高度作为WRAP\u内容。这段代码帮助我实现了动态listview高度
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.widget.ListAdapter;
import android.widget.ListView;
public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
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();
}
}
phraseListView=(ListView)findViewById(R.id.phrase_listview);
phraseAdapter=new PhraseListAdapter(this);
phraseListView.setAdapter(phraseAdapter);
Utility.setListViewHeightBasedOnChildren(phraseListView);
摘自您可以使用以下方法根据您的项目以编程方式设置listview的高度:
<ListView
android:id="@+id/lvMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
/>
希望它能对您有所帮助。首先要记住的是,当使用mylistview.getmeasureheight获取listview的高度时,它总是根据初始化适配器期间设置的列表中的textview/View给出高度。因此,我们需要如下声明适配器:
myadapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myarraylist){
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = ((TextView) view.findViewById(android.R.id.text1));
textView.setMinHeight(0); // Min Height
textView.setMinimumHeight(0); // Min Height
textView.setHeight(100); // Height
return view;
}
};
myarraylist.add(ds.getKey());
myadapter.notifyDataSetChanged();
mylist.setAdapter(myadapter);
setListViewHeightBasedOnItems(mylist);
现在,每次向列表中添加项目后,按如下方式调用函数:
myadapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myarraylist){
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = ((TextView) view.findViewById(android.R.id.text1));
textView.setMinHeight(0); // Min Height
textView.setMinimumHeight(0); // Min Height
textView.setHeight(100); // Height
return view;
}
};
myarraylist.add(ds.getKey());
myadapter.notifyDataSetChanged();
mylist.setAdapter(myadapter);
setListViewHeightBasedOnItems(mylist);
将同一类中的函数定义为:
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);
float px = 500 * (listView.getResources().getDisplayMetrics().density);
item.measure(View.MeasureSpec.makeMeasureSpec((int)px, View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalItemsHeight += item.getMeasuredHeight();
}
// Get total height of all item dividers.
int totalDividersHeight = listView.getDividerHeight() *
(numberOfItems - 1);
// Get padding
int totalPadding = listView.getPaddingTop() + listView.getPaddingBottom();
// Set list height.
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalItemsHeight + totalDividersHeight + totalPadding;
listView.setLayoutParams(params);
listView.requestLayout();
return true;
} else {
return false;
}
}
public静态布尔setListViewHeightBasedOnItems(ListView ListView){
ListAdapter ListAdapter=listView.getAdapter();
if(listAdapter!=null){
int numberOfItems=listAdapter.getCount();
//获取所有项目的总高度。
int totalItemsHight=0;
对于(int-itemPos=0;itemPos
您还可以通过返回真/假来检查高度设置是否正确。这个解决方案对我有效。希望这有帮助 您必须创建一个新控件,将listview扩展到某些函数上
public class WrapContentListView extends ListView {
public WrapContentListView(Context context) {
super(context);
}
public WrapContentListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public WrapContentListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
@Override
public void setAdapter(ListAdapter adapter) {
super.setAdapter(adapter);
setHeightWrapContent();
}
public void setHeightWrapContent() {
ListAdapter listAdapter = getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, this);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = this.getLayoutParams();
params.height = totalHeight
+ (this.getDividerHeight() * (listAdapter.getCount() - 1));
this.setLayoutParams(params);
}
}
公共类WrapContentListView扩展了ListView{
公共WrapContentListView(上下文){
超级(上下文);
}
公共WrapContentListView(上下文上下文、属性集属性){
超级(上下文,attrs);
}
公共WrapContentListView(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
}
@凌驾
测量时的公共空隙(int-widthMeasureSpec,int-heightMeasureSpec){
int expandSpec=MeasureSpec.makeMeasureSpec(Integer.MAX_值>>2,
测量(最多);
超级测量(宽度测量规范、扩展规范);
}
@凌驾
public void setAdapter(ListAdapter适配器){
super.setAdapter(适配器);
setHeightWrapContent();
}
public void setHeightWrapContent(){
ListAdapter ListAdapter=getAdapter();
如果(listAdapter==null){
返回;
}
int totalHeight=0;
对于(int i=0;i
就我个人而言,我花了很多时间试图找到一个解决方案,结果我所要做的就是设置我的listview的高度来包装内容。上面的解决方案给了我一个选角错误,改为LinearLayout效果很好。LinearLayout.LayoutParams mParam=新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_父项,myList.size()*100);listViewPhone.setLayoutParams(mParam)@Luiji博士,我使用了类似这样的东西
RelativeLayout.LayoutParams p=newrelativelayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,item.getRoutes().size()*200)代码>。但我无法看到列表中的某些项目。任何建议,我想显示整个列表项的列表。我能做什么?完美。谢谢你,汉克斯,这对我也有用。实际上,我在scrollview中以一种大的形式使用Listview。这就是为什么我必须使用此代码,listview高度属性中的其他wrap_内容值效果最好。不管怎样,很好的实用工具干得好:)你为什么要乘以500?执行“浮点px=500*(listView.getResources().getDisplayMetrics().density);”的原因是什么??
public class WrapContentListView extends ListView {
public WrapContentListView(Context context) {
super(context);
}
public WrapContentListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public WrapContentListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
@Override
public void setAdapter(ListAdapter adapter) {
super.setAdapter(adapter);
setHeightWrapContent();
}
public void setHeightWrapContent() {
ListAdapter listAdapter = getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, this);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = this.getLayoutParams();
params.height = totalHeight
+ (this.getDividerHeight() * (listAdapter.getCount() - 1));
this.setLayoutParams(params);
}
}