2013-02-18 86 views
1

我目前正在編寫一個Android項目,但我在我的xml文件中遇到問題。沒有日期選擇器,listview運行良好。但是,當我添加日期選擇器(不編輯java文件)時,listview消失。以下是我的android xml文件:當添加DatePicker時,ListView消失

<?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Ÿ二重奏甚至出現與日期選擇器列表視圖,但仍然沒有出現在三星Galaxy Note

回答

0

我的猜測是,ListView控件之前的小部件最終以所有的垂直空間和你的列表視圖只是沒有空間出現。嘗試將TextView,Button和DatePicker的高度設置爲10dp,然後查看是否可以看到列表。如果可以的話,它的空間問題,你應該考慮使用ScrollView而不是LinearLayout

+0

試過ScrollView但無濟於事。我也嘗試將DatePicker的高度設置爲10dp,但ListView仍然不顯示。但是,當我刪除Datepicker,甚至增加TextVIew和Button的大小時,ListView出現。 – user1699366 2013-02-18 06:02:16

+0

發現我上面貼出的代碼適用於Samsung Galaxy Y,但不適用於Samsung Galaxy Note。這是什麼意思? – user1699366 2013-02-18 06:03:53

+0

您的父母'LinearLayout'似乎沒有結束標記。這只是一個錯誤複製..或?另外,你不需要將'ListView'包裝在'LinearLayout'中。只需將它作爲父'LinearLayout'的子元素,並將其高度設置爲「wrap_content」。 – Akash 2013-02-18 06:21:49

0

除了從頂部佈局,請刪除此行

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

0

嘗試將重量分配給您的佈局,看看。 將android:layout_weight =「1」添加到兩個線性佈局(不要將此添加到您的主要線性佈局)

1

我有一個類似的問題,當我使用DatePicker和ListView與自定義適配器相同的佈局,並與我的活動擴展ListActivity和調用getListView()。該列表出現在較舊版本的Android上,但不在較新版本上。我經歷過我的適配器的getView()方法沒有被調用。

我把它通過改變Android的較新版本的工作:ID =「@機器人:ID /列表」,以自定義的ID,並調用findViewById這個編號,但隨後的舊版本有一個「RuntimeException的:你的內容必須有一個ListView,其id屬性是'android.R.id.list'「。爲了使它在舊版本和新版本上都能工作,我使用了自定義標識和findViewById方法,但是從Activity擴展而不是ListActivity。也許有辦法在不避開ListActivity的情況下處理這個問題,但至少對我來說這很有效。