2011-08-22 38 views
1

對不起,我是一個新手:(Android的添加按鈕前的ListView

如何在ListView頂部添加Button和風格喜歡這裏: This Image

+1

可以參考這個[link](http://blog.maxaller.name/2010/05/attaching-a-sticky-headerfooter-to-an-android-listview/) –

+0

[This](https:/ /android.googlesource.com/platform/packages/apps/DeskClock/+/gingerbread-release)是薑餅原始鬧鐘應用程序的源代碼。您可以看到使用的按鈕樣式不是標準樣式。 – danieleds

回答

2

我認爲i'ts更好,當你第一次使用的LinearLayout與垂直方向,並把一個按鈕,並把一個ListView。在的LinearLayout將您的 「內容包裝」 是這樣的: main.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"> 
    <Button 
     android:text="Button" 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <ListView 
     android:id="@+id/listView1" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:entries="@array/list"/>  
</LinearLayout> 

和字符串數組項:

<string-array name="list"> 
    <item>Entry1</item> 
    <item>Entry2</item> 
    <item>Entry3</item> 
    <item>Entry4</item> 
</string-array> 
0

首先,我只是想確認,是Button始終,即使ListView滾動的ListView頂部,該Button仍然存在或Button滾動過也

如果是第一種情況,這是很簡單的,J烏斯特垂直LinearLayoutButton之前ListView

如果是第二種情況,它是相當複雜的。因爲你不應該在ScrollView裏面使用ListView,所以我建議你讓ListView的第一行成爲一個內部有Button的自定義視圖。爲了做到這一點,請查看如何擴展BaseAdapterLayoutInflate

0

也許這個按鈕不是列表視圖的一部分。 嘗試撥打佈局是這樣的:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 
    <ListView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 
</LinearLayout> 
+0

'ListView'餓了高度,並會填充'LinearLayout'中所有可用的高度。 –

0

可以使用RelativeLayout的做到這一點:

<RelativeLayout android:id="@+id/parentLayout" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

    <Button android:id="@+id/btn" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="My Button" 
     android:alignParentTop = "true"/> 

    <ListView android:id="@+id/myList" 
    android:layout_below="@id/btn" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 

</RelativeLayout> 
0
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_gravity="top|center_horizontal" 
    android:layout_width="match_parent" android:layout_height="wrap_content"> 
    <Button android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:text="Add Alarm"/> 
    <ListView android:layout_width="fill_parent" android:layout_height="0dip" 
     android:layout_weight="1.0"></ListView> 
</LinearLayout> 

這應該給出所需的佈局。

+1

這項工作。但風格不像這個形象 – Rose

0

您可以設計自己的佈局中正在適配器中充氣的按鈕,並將按鈕可見性消除。在適配器中,如果psoition爲0,並且如果爲0,則只能在getview方法中檢查按鈕,否則使按鈕變爲可見。這樣,您在列表頂部有一個按鈕,並且可以滾動。要使按鈕可點擊,只需在getview方法中實現onclick方法。它非常簡單。

相關問題