添加DatePicker时ListView消失

时间:2013-02-18 05:45:37

标签: java android xml listview datepicker

我目前正在编写Android项目,但我的xml文件出现问题。没有datepicker,listview运行良好。但是,当我添加一个datepicker(不编辑java文件)时,listview会消失。以下是我的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音符

4 个答案:

答案 0 :(得分:1)

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

我通过将android:id =“@ android:id / list”更改为自定义ID,并使用此id调用findViewById,然后旧版本获得“RuntimeException:您的内容必须有一个ListView,其id属性是'android.R.id.list'“。为了使它适用于旧版本和新版本,我使用了自定义id和findViewById方法,但是从Activity而不是ListActivity扩展。也许有办法在不避开ListActivity的情况下解决这个问题,但这至少对我有用。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

请删除此行

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

除了顶部布局。

答案 3 :(得分:0)

尝试为布局指定权重并查看。 将android:layout_weight =“1”添加到两个线性布局中(不要将其添加到主线性布局)