开发者

How to set height for the listview that created dynamically through java code?

开发者 https://www.devze.com 2023-04-07 21:26 出处:网络
I \'m trying to set the height of the listview which has been created through java code dynamically. I\'m using 3 listviews(one below another) in a scrollview(to avoid hiding of lists). If i use scrol

I 'm trying to set the height of the listview which has been created through java code dynamically. I'm using 3 listviews(one below another) in a scrollview(to avoid hiding of lists). If i use scrol开发者_运维知识库l view, i cannot see all the list items of the listviews.Is there any idea to set height for those 3 list views through java coding in android? Any help is really appreciated and thanks in advance...


Don't put ListViews in ScrollViews! In Detail Information from the developers of Listview is available in the talk World of ListViews

It is possible to make the three ListViews share the space on the Screen by using the layoutWeight parameter.

Make a layout like this (you can also use code for that):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/listView1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1"></ListView>
<ListView android:id="@+id/listView2" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1"></ListView>
<ListView android:id="@+id/listView3" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1"></ListView>
</LinearLayout>

Here each ListView takes a third of the available space (all weights set to 1).

To set the layoutWeight in code use this line as example:

listview1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
0

精彩评论

暂无评论...
验证码 换一张
取 消