2014-12-03 30 views
0

我在下面的代碼顯示了我想要做的。我想將視圖作爲頁腳插入到ListView中。不過,我似乎只能分別輸入編輯文本或按鈕,而不能將其作爲一個整體視圖。我知道這聽起來很混亂。謝謝你的幫助。如何創建一個包含編輯文本和按鈕的視圖?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="200dp" 
android:layout_height="200dp" 
android:layout_gravity="center" 
android:background="@drawable/bg_card" 
android:gravity="center" 
android:orientation="horizontal" 
android:padding="10dp" > 

<EditText 
    android:id="@+id/txtTitle" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginBottom="5dp" 
    android:layout_marginTop="5dp" 
    android:gravity="center" 
    android:textColor="#000000" 
    android:textSize="22dp" 
    android:textStyle="bold" /> 


<Button 
    android:id="@+id/newButton" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" /> 

</LinearLayout> 

回答

3

充氣的佈局:

視圖v = LayoutInflator.from(上下文).inflate(R.layout.footer,NULL);

並將其添加爲頁腳到ListView:

listview.addFooterView至(v)

編輯: 如果你的目標API級別<奇巧,你必須調用

listview.setAdapter之前調用addFooterView();

+0

這是工作!謝謝 – user3460821 2014-12-03 02:42:22

+0

很高興幫助。你認爲你可以通過點擊複選框來接受答案嗎?謝謝。 – 2014-12-03 08:34:19

0

我不知道有任何使LinearLayout成爲頁腳視圖的問題。但是,您的XML確實有一些問題。

  • 雖然父級是LinearLayout,但您有一些RelativeLayout參數。
  • 這兩個子視圖都是fill_parent寬度。最好讓它們都是wrap_content寬度,並使用layout_weight來填充任何額外的空間。
  • 由於您在父項中有填充,因此您可能不需要EditText中的頂部/底部邊距。
  • 重力作爲LinearLayout的屬性,但不適用於該視圖。你也有layout_gravity,這很好:它說明這個LinearLayout如何適合父級ListView。它會將標題置於中心。

下面是這些補丁版本:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="200dp" 
    android:layout_height="200dp" 
    android:layout_gravity="center" 
    android:background="@drawable/bg_card" 
    android:orientation="horizontal" 
    android:padding="10dp" > 

    <EditText 
     android:id="@+id/txtTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:textColor="#000000" 
     android:textSize="22dp" 
     android:textStyle="bold" /> 
    <Button 
     android:id="@+id/newButton" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="center" /> 
</LinearLayout> 

如果你仍然看到問題,也許你可以描述顯示的內容。

+0

感謝您的XML編輯 – user3460821 2014-12-03 02:42:04

相關問題