【问题标题】:ListView disappears when DatePicker is added添加 DatePicker 时 ListView 消失
【发布时间】:2013-02-18 05:45:37
【问题描述】:

我目前正在编写一个 Android 项目,但我的 xml 文件有问题。没有日期选择器,列表视图运行良好。但是,当我添加一个日期选择器(不编辑 java 文件)时,列表视图消失了。以下是我的android xml文件:

<?xml version="1.0" encoding="utf-8"?>    
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:orientation="vertical" >


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Date"
        android:textColor="@android:color/white"
        android:textSize="25sp" />

  <DatePicker
        android:id="@+id/datePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/> 

     <Button
        android:id="@+id/confirm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Confirm" /> 

</LinearLayout>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@android:color/background_dark" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false" />
</LinearLayout>

</LinearLayout>

我的 xml 文件有问题吗?

更新: 我发现即使使用日期选择器,列表视图也会出现在三星 Galaxy Y Duos 上,但仍然没有出现在三星 Galaxy Note 上

【问题讨论】:

    标签: java android xml listview datepicker


    【解决方案1】:

    当我在同一布局中使用带有自定义适配器的 DatePicker 和 ListView,并且我的活动扩展 ListActivity 并调用 getListView() 时,我遇到了类似的问题。该列表出现在旧版本的 Android 上,但不在新版本上。我发现我的适配器的 getView() 方法没有被调用。

    我通过将 android:id="@android:id/list" 更改为自定义 id 并使用此 id 调用 findViewById 让它在新版本上工作,但是旧版本得到了“RuntimeException:您的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView”。为了使它适用于新旧版本,我使用了自定义 id 和 findViewById 方法,但从 Activity 而不是 ListActivity 扩展。也许有一些方法可以在不避免 ListActivity 的情况下处理这个问题,但这至少对我有用。

    【讨论】:

      【解决方案2】:

      我的猜测是 ListView 之前的小部件最终占据了所有垂直空间,而您的列表视图没有空间出现。尝试将 TextView、Button 和 DatePicker 的高度设置为 10dp 之类的,然后看看是否可以看到列表。如果可以,这是空间问题,您应该考虑使用ScrollView 而不是LinearLayout

      【讨论】:

      • 试过 ScrollView 但无济于事。我还尝试将 DatePicker 的高度设置为 10dp,但 ListView 仍然没有显示。但是,当我删除 Datepicker 甚至增加 TextVIew 和 Button 的大小时,就会出现 ListView。
      • 发现我上面发布的代码适用于三星 Galaxy Y,但不适用于三星 Galaxy Note。这是什么意思?
      • 你的父母LinearLayout 似乎没有结束标签。这只是复制的错误..还是?此外,您实际上不需要将ListView 包装在LinearLayout 中。只需将其作为父 LinearLayout 的子级并将其高度设置为“wrap_content”即可。
      • 对不起。那是抄袭的错误。这个版本实际上不需要,但我计划稍后添加一个按钮。尽管如此,即使没有将 ListView 包装在 LinearLayout 中,ListView 仍然不会出现。
      • 好的。我尝试在项目中运行您的布局并在子元素中声明命名空间会导致 lint 引发错误。尝试从除父 LinearLayout 之外的所有元素中删除“xmlns:android="schemas.android.com/apk/res/android"。除此之外,您的布局对我有用。
      【解决方案3】:

      尝试为您的布局分配权重并查看。 将 android:layout_weight="1" 添加到您的两个线性布局中(不要将其添加到您的主要线性布局中)

      【讨论】:

        【解决方案4】:

        请删除此行

        xmlns:android="http://schemas.android.com/apk/res/android
        

        除了顶部布局。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-05
          • 1970-01-01
          • 1970-01-01
          • 2017-09-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多