2015-02-11 99 views
0

我有Android布局(如下所示)。 我设置了一个像这样的侦听器:list.setOnItemClickListener。 一切似乎都很好。Android列表视图setOnItemClickListener仅适用于

在下面的一行:

<ScrollView android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 

如果我改变安卓:layout_height从“WRAP_CONTENT”到“FILL_PARENT”它不工作了(从我的列表中的项目不能被选中)。
它只有在设置为wrap_content时才有效。 有人可以解释为什么?!??

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    <TabWidget android:id="@android:id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
      /> 
    <FrameLayout android:id="@android:id/tabcontent" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
      > 
     <ListView android:id="@+id/restaurants" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
       /> 
     <ScrollView android:layout_width="fill_parent" 
        android:layout_height="wrap_content"> 

      <TableLayout android:id="@+id/details" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:stretchColumns="1" 
         android:shrinkColumns="1" 
         android:paddingTop="4dip" 
        > 
       <TableRow> 
        <TextView android:text="@string/name" /> 
        <EditText android:id="@+id/name" /> 
       </TableRow> 
       <TableRow> 
        <TextView android:text="@string/address" /> 
        <EditText android:id="@+id/addr" /> 
       </TableRow> 
       <TableRow> 
        <TextView android:text="Type:" /> 
        <RadioGroup android:id="@+id/types"> 
         <RadioButton android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:id="@+id/take_out" 
            android:text="@string/takeout" 
            android:checked="true" 
           /> 

         <RadioButton android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:id="@+id/sit_down" 
            android:text="@string/sitdown" 
           /> 
         <RadioButton android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:id="@+id/delivery" 
            android:text="@string/delivery" 
           /> 
        </RadioGroup> 
       </TableRow> 

       <TableRow> 
        <TextView android:text="@string/notes" /> 
        <EditText android:id="@+id/notes" /> 
       </TableRow> 

       <Button android:id="@+id/save" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/save" 
         /> 
      </TableLayout> 
     </ScrollView> 
    </FrameLayout> 
</LinearLayout> 

+0

它不会工作,因为使用'fill_parent'将尝试填充'FrameLayout'内的所有区域,但也存在'ListView android:id =“@ + id/restaurants”'。也许一个解决方案将除了'ScrollView'在另一个'RelativeView',并将其放置在ListView android:id =“@ + id/restaurants”' – hrskrs 2015-02-11 11:50:55

+0

做一个测试:将listView背景颜色更改为黑色,将ScrollView背景颜色更改为红。看看你的工作看法。它可以填充listView上方的所有空间。 – 2015-02-11 12:21:35

回答

0

fill_parent(弃用,在API级别8改名为MATCH_PARENT及更高版本)

设置一个小部件的布局FILL_PARENT将迫使它扩大到占据尽可能多的它们的布局元素中可用的空间大致相当于将Windows窗体控件的dockstyle设置为Fill

将顶层布局或控件设置为fill_parent将强制它占据整个屏幕。

wrap_content

设置视图的尺寸为wrap_content将迫使它扩大只是远远不够,以遏制它所包含的值(或子控件)。对于控件 - 如文本框(TextView)或图像(ImageView) - 这将包装显示的文本或图像。对于布局元素,它将调整布局大小以适合添加为其子元素的控件/布局。

在Android代码文档here中有一些细节。

0

您的ScrollView占用了ListView中的所有可用空间,这些空间可以填充到屏幕上,基本上覆盖了它。

您绝对不应该将ScrollView与ListView一起使用,因为ListView会负责自己的垂直滚动。最重要的是,这样做会挫败ListView中用于处理大型列表的所有重要优化,因为它有效地强制ListView显示其整个项目列表以填充由ScrollView提供的无限容器。 - 从http://developer.android.com/reference/android/widget/ScrollView.html

相关问题