Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当组件位于listview上方时,无法使用自定义行填充listview_Android_Listview_Button_Row - Fatal编程技术网

Android 当组件位于listview上方时,无法使用自定义行填充listview

Android 当组件位于listview上方时,无法使用自定义行填充listview,android,listview,button,row,Android,Listview,Button,Row,我在使用从数据库加载的自定义行的ListView时遇到问题 如果在列表屏幕中,我在ListView上方放置了一个按钮,则ListView中不会显示任何可见行 然而,只要我卸下按钮,一切都正常。我希望按钮或任何其他组件出现在上面,使其更加用户友好。附件是下面的两个代码示例 这是有效的ListView活动的XML文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schem

我在使用从数据库加载的自定义行的ListView时遇到问题

如果在列表屏幕中,我在ListView上方放置了一个按钮,则ListView中不会显示任何可见行

然而,只要我卸下按钮,一切都正常。我希望按钮或任何其他组件出现在上面,使其更加用户友好。附件是下面的两个代码示例

这是有效的ListView活动的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@color/real_red_dark">
  <LinearLayout
    android:id="@+id/llMain"
    android:layout_height="fill_parent"
    android:background="@drawable/real_background"
    android:layout_width="fill_parent"
    android:layout_gravity="center"
    android:gravity="center">
        <ListView 
            android:id="@+android:id/list"      
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/llButton"
            android:layout_alignParentLeft="true" />        
        <TextView
            android:id="@+android:id/empty"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="@string/no_sessions"
            android:textStyle="bold" 
            android:textSize="18dp" 
            android:gravity="center"/>
    </LinearLayout>
</LinearLayout>
但是,如果我在其上方添加了按钮,它将不会显示任何内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@color/real_red_dark">
  <LinearLayout
    android:id="@+id/llMain"
    android:layout_height="fill_parent"
    android:background="@drawable/real_background"
    android:layout_width="fill_parent"
    android:layout_gravity="center"
    android:gravity="center">
                <Button 
        android:id="@+id/btnSearch"
        android:layout_width="fill_parent"
        android:layout_height="35dip"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="3dip"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="15dip"
        android:text="Find Sessions"
        android:textStyle="bold" />
        <ListView 
            android:id="@+android:id/list"      
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/llButton"
            android:layout_alignParentLeft="true" />        
        <TextView
            android:id="@+android:id/empty"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="@string/no_sessions"
            android:textStyle="bold" 
            android:textSize="18dp" 
            android:gravity="center"/>
    </LinearLayout>
</LinearLayout>

如果您使用RelativeLayout比使用LinearLayout更好,android文档也建议使用它。尝试使用android:layout高度值作为ListView和TextView的换行内容,您可以使用它来指示空行,这可能会对您有所帮助。

您可以尝试以编程方式将按钮添加为ListView本身的标题视图,而不是在xml布局中

使用listView.addHeaderViewView


有些东西看起来不对劲,但我想这只是上面的一个打字错误

android:id="@+android:id/list"
…在@和android之间不应该有+符号:。使用@+用于添加您自己的新资源id,即@+id:。你也在为TextView做同样的事情

android:id="@+android:id/empty"
另一件事,但不确定它的相关性是你指定

android:layout_below="@+id/llButton"
…我怀疑这是问题所在,因为android:layout_对于LinearLayout无效,它用于RelativeLayout,但在您的布局中没有id为llButton的按钮。如果存在,则+也将不正确,因为您应该指定一个现有id


不确定修改上述内容是否会解决问题,但可能只是因为这些问题,布局膨胀出现了“错误”。

这对我来说是有效的,但是如果没有从数据库添加或下载项目,则标题不会显示任何内容。有什么想法吗?@John:也许你可以用setEmptyView。。。表示没有可用的数据。它可以解决收割台未显示的问题。看,那是我的错误。那是我搞布局时留下的。谢谢你的接球!