代码之家  ›  专栏  ›  技术社区  ›  SK16

列表视图最后一项可见性超出屏幕

  •  0
  • SK16  · 技术社区  · 10 年前

    我正在使用滚动视图和网格布局。只有自动完成文本视图、1个按钮和结果列表视图。但列表视图上的最后一项将像

    enter image description here

    我的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>
    

    `

    需要一些修改布局的建议。谢谢。

    编辑:我刚刚删除了滚动视图并运行项目。。。但问题仍然存在。。输出如下

    enter image description here

    如您所见,listview中的最后一项显示了一半或1/3。无法从DB操作中跳过它。

    2 回复  |  直到 10 年前
        1
  •  1
  •   user3956566 user3956566    10 年前

    我在互联网上找到了这个实用程序类,但记不起是谁创建的,如果我能找到原始源,将提供属性。我修改了一下。

    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>
    
        2
  •  1
  •   Ramesh    10 年前

    您可以使用相对布局,而不是使用网格布局。尝试使用以下代码:

    <?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>