我的listview在设计方面存在问题,因为它让我在底部留出一些额外的空间,实际问题是我在一个活动中使用多个listviews,然后控制每个listview的高度使用此代码: -
public static void setListViewHeightBasedOnChildren(ListView listView,Context c) {
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);
if (listItem instanceof ViewGroup)
listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
// Toast.makeText(c, "height:-"+totalHeight, 0).show();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
Toast.makeText(c, "height:-"+ params.height, 0).show();
listView.setLayoutParams(params);
listView.requestLayout();
}
它工作正常,当我从json在线加载数据时,但是当我从sqlite数据库离线使用我的数据时,它将listview的高度不必要地增加到项目高度的三倍,我用Google搜索了很多,但没有得到任何东西,如果有人有一些想法,请告诉我,我可以解释一下。
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#F45555" >
<TextView
android:id="@+id/category1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Breaking News"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/white"
android:textSize="16dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:background="@color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#F45555" >
<TextView
android:id="@+id/category1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#F45555"
android:text="Top Stories"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/white"
android:textSize="16dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/list2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#F45555" >
<TextView
android:id="@+id/category1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="#F45555"
android:text="City"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/white"
android:textSize="16dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#F45555" >
<TextView
android:id="@+id/category1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="News"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/white"
android:textSize="16dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#F45555" >
<TextView
android:id="@+id/category1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kashmir"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/white"
android:textSize="16dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#F45555" >
<TextView
android:id="@+id/category1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F45555"
android:text="Jammu"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/white"
android:textSize="16dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@color/white" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
试试这个:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#F45555" >
<TextView
android:id="@+id/category1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Breaking News"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/white"
android:textSize="16dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#F45555" >
<TextView
android:id="@+id/category1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#F45555"
android:text="Top Stories"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/white"
android:textSize="16dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ListView
android:id="@+id/list2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#F45555" >
<TextView
android:id="@+id/category1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#F45555"
android:text="City"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/white"
android:textSize="16dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ListView
android:id="@+id/list3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#F45555" >
<TextView
android:id="@+id/category1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="News"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/white"
android:textSize="16dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ListView
android:id="@+id/list4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#F45555" >
<TextView
android:id="@+id/category1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Kashmir"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/white"
android:textSize="16dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ListView
android:id="@+id/list5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#F45555" >
<TextView
android:id="@+id/category1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#F45555"
android:text="Jammu"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/white"
android:textSize="16dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ListView
android:id="@+id/list6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@color/white" />
</LinearLayout>
答案 1 :(得分:0)
请调试从遥控器获取的数据。它包含额外的空格。