我正在使用scrollview和gridlayout。只有autocompleteTextview,1个按钮和结果列表视图。但是列表视图上的最后一项是像
那样走出屏幕
我的XML代码如下
`
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="1" >
<AutoCompleteTextView
android:id="@+id/AutoText_dest"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="0"
android:ems="10"
android:hint="Desired Destination"
android:imeOptions="actionDone"
android:singleLine="true" >
<requestFocus />
</AutoCompleteTextView>
<Button
android:id="@+id/btn_load_directions"
style="?android:attr/buttonStyleSmall"
android:layout_column="0"
android:layout_gravity="right|top"
android:layout_row="0"
android:background="@drawable/ic_action_search"
/>
<ListView
android:id="@+id/Listview_search_results"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="fill_vertical"
android:layout_marginTop="10dp"
android:layout_row="1" >
</ListView>
</GridLayout>
`
需要一些修改布局的建议。 Thanx in adv。
编辑:我刚刚删除了scrollview并运行了项目......但仍然存在问题..输出就像这样
如您所见,listview中的最后一项显示为一半或1/3。不能跳过它作为数据库操作的结果。
答案 0 :(得分:1)
我在互联网上发现了这个实用工具类,无法回想起创建它的人,如果我能找到原始资源,则会提供属性。我修改了一下。
public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
//I added this to try to fix half hidden row
totalHeight++;
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
}
如何实现此课程。
ListView lv;
ArrayAdapter adapter;
....
lv.setAdapter(adapter);
//set height to see all items - hack because of listview in scrollable view
Utility.setListViewHeightBasedOnChildren(lv);
xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
....>
<ScrollView
....
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true">
<LinearLayout
....
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
....
android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
答案 1 :(得分:1)
您可以使用相对布局,而不是使用网格布局。试试以下代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<AutoCompleteTextView
android:id="@+id/AutoText_dest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Desired Destination"
android:imeOptions="actionDone"
android:singleLine="true" >
<requestFocus />
</AutoCompleteTextView>
<Button
android:id="@+id/btn_load_directions"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/ic_launcher" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/AutoText_dest"
android:layout_marginTop="10dp" >
</ListView>
</RelativeLayout>