[英]Android Add Button Top Of Listview
我認為當您首先以垂直方向使用LinearLayout並放置一個Button和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>
您可以嘗試ListView.addHeaderView();
這樣,按鈕將出現在元素上方的ListView中。 另外,請看一下本教程 。
您可以在自己的布局中設計要在適配器中膨脹的按鈕,並設置按鈕的可見性消失。 在適配器中,您僅必須在getview方法中檢查psoition是否為0,如果psoition為0,則使按鈕可見,否則使其消失。 這樣,您可以在列表頂部找到一個按鈕,並且可以滾動。 要使按鈕可單擊,請僅在getview方法中實現onclick方法。 這很容易。
首先,我只是想確認,是Button
總是在頂部ListView
,即使ListView
滾動時, Button
仍然存在或Button
滾動過也
如果是第一種情況,則非常簡單,只需在ListView
之前使用Button
創建一個垂直的LinearLayout
如果是第二種情況,則相當復雜。 由於您不應該在ScrollView
內使用ListView
,因此建議您將ListView
的第一行設置為內部包含Button
的自定義視圖。 為了做到這一點,請查看如何擴展BaseAdapter
和LayoutInflate
。
也許此按鈕不是列表視圖的一部分。 嘗試進行如下布局:
<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>
您可以使用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>
<?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>
這應該給出所需的布局。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.