Android 在运行时更改ListView高度
我试图在UI的底部实现一个嵌套的ListView,但是只有一个数组项填充了屏幕。我发现,如果我设置了高度属性(即“175dp”而不是“Wrap_Content”),我就可以显示我的所有项目。问题是列表大小不是静态的,所以我需要在运行时调整ListView的高度 我已经在互联网和StackOverflow上搜索过了,但似乎找不到解决办法 谢谢 我的片段中的代码:Android 在运行时更改ListView高度,android,listview,android-fragments,android-listview,Android,Listview,Android Fragments,Android Listview,我试图在UI的底部实现一个嵌套的ListView,但是只有一个数组项填充了屏幕。我发现,如果我设置了高度属性(即“175dp”而不是“Wrap_Content”),我就可以显示我的所有项目。问题是列表大小不是静态的,所以我需要在运行时调整ListView的高度 我已经在互联网和StackOverflow上搜索过了,但似乎找不到解决办法 谢谢 我的片段中的代码: for (int i = 0; i < values.length; ++i) { list.add(values[i])
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(rootView.getContext(),
android.R.layout.simple_list_item_1, list);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) rootView.findViewById(R.id.list_linear).getLayoutParams();
LinearLayout manager = (LinearLayout) rootView.findViewById(R.id.list_linear);
lp.height = 45 * values.length;
manager.setLayoutParams(lp);
listView.setAdapter(adapter);
for(int i=0;i
我的XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#BBDEFB"
android:padding="3dp"
android:id="@+id/movie_description_container">
<ImageView
android:layout_width="match_parent"
android:layout_height="225dp"
android:id="@+id/movie_detail_hero_image"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:gravity="top"
android:src="@drawable/sample_1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="movie title"
android:id="@+id/movie_detail_title"
android:layout_alignBottom="@+id/movie_detail_hero_image"
android:backgroundTint="@color/background_floating_material_dark"
android:textSize="36dp"
android:textColor="#BBDEFB"
android:background="#a3000000"
android:layout_above="@id/movie_detail_hero_image"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:paddingLeft="18dp"/>
<ImageView
android:layout_width="175dp"
android:layout_height="215dp"
android:id="@+id/movie_detail_poster"
android:src="@drawable/sample_0"
android:layout_below="@+id/movie_detail_title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_margin="12dp"
android:gravity="left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="date"
android:textSize="22dp"
android:id="@+id/movie_detail_date"
android:layout_centerVertical="true"
android:layout_below="@id/movie_detail_hero_image"
android:layout_alignRight="@+id/movie_detail_hero_image"
android:layout_marginRight="12dp"
android:layout_marginTop="32dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Rating:"
android:textSize="22dp"
android:id="@+id/movie_detail_rating"
android:layout_below="@+id/movie_detail_date"
android:layout_alignRight="@+id/movie_detail_date"
android:layout_alignEnd="@+id/movie_detail_date"
android:layout_marginTop="6dp" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ratingBar"
android:numStars="4"
android:nestedScrollingEnabled="true"
android:maxWidth="77dp"
android:layout_below="@+id/movie_detail_rating"
android:layout_alignRight="@+id/movie_detail_rating"
android:layout_alignEnd="@+id/movie_detail_rating"
android:layout_marginTop="3dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Synopsis"
android:id="@+id/movie_detail_synopsis_header"
android:layout_below="@+id/movie_detail_poster"
android:layout_alignLeft="@+id/movie_detail_poster"
android:layout_alignStart="@+id/movie_detail_poster" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="description"
android:id="@+id/movie_detail_synopsis"
android:layout_below="@+id/movie_detail_synopsis_header"
android:layout_alignLeft="@+id/movie_detail_synopsis_header"
android:layout_alignStart="@+id/movie_detail_synopsis_header"
android:layout_margin="6dp"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton_Favorite"
android:layout_below="@+id/ratingBar"
android:layout_alignRight="@+id/ratingBar"
android:src="@drawable/ic_favorite_white_24dp"
android:maxWidth="45dp"
android:maxHeight="45dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_linear"
android:layout_below="@id/movie_detail_synopsis">
<ListView
android:layout_width="match_parent"
android:layout_height="45dp"
android:id="@+id/list_view2"/>
</LinearLayout>
实际上,您可以使用此方法将listview的高度设置为childers的大小
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(),
MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,
LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
public static void setListViewHeightBasedOnChildren(ListView ListView){
ListAdapter ListAdapter=listView.getAdapter();
如果(listAdapter==null)
返回;
int desiredWidth=MeasureSpec.makeMeasureSpec(listView.getWidth(),
测量(未指定);
int totalHeight=0;
视图=空;
对于(int i=0;i
实际上,您可以使用此方法将listview的高度设置为childers的大小
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(),
MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,
LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
public static void setListViewHeightBasedOnChildren(ListView ListView){
ListAdapter ListAdapter=listView.getAdapter();
如果(listAdapter==null)
返回;
int desiredWidth=MeasureSpec.makeMeasureSpec(listView.getWidth(),
测量(未指定);
int totalHeight=0;
视图=空;
对于(int i=0;i
您尚未为包含列表视图的线性布局设置方向。默认值为水平。此外,列表视图是可滚动的。您不必将列表放得更高,以便为更多项目腾出空间。您没有为包含列表视图的LinearLayout设置方向。默认值为水平。此外,列表视图是可滚动的。你不必为了给更多的物品腾出空间而把清单列得更高。