簡體   English   中英

如何將按鈕添加到列表視圖中的每個項目

[英]how to add button to every item in a list view

我有一個帶listView的布局

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/testMain"
     android:layout_width="fill_parent"         
     android:layout_height="fill_parent" >



    <ListView
         android:id="@+id/notesListView"
        android:layout_width="480dp"
        android:layout_height="wrap_content"
        android:background="#000"
        android:divider="#FFF"
        android:dividerHeight="5dp"         
        android:fadingEdge="none"
        android:overScrollFooter="#000"
        android:layout_marginBottom="15dp"         
         />              
 </RelativeLayout>

我使用ListView_Adapter將項目動態添加到列表中。 是否可以在列表的每一行的右角添加一個按鈕?

是的,您必須使用自定義listview。在自定義ListView中,必須從布局xml文件動態添加listitem。

范例:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/testMain"
     android:layout_width="fill_parent"         
     android:layout_height="fill_parent" >

    <Button
         android:id="@+id/notesListView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_alignParentRight="true"

         />              
 </RelativeLayout>

使您的listitem xml成為

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp">

<TextView
 android:id="@+id/title"
 android:textColor="#FFF"
 android:layout_width="120dp"
 android:layout_height="wrap_content"/>

 <Button
 android:id="@+id/Btn01"
  android:textColor="#000"
  android:layout_weight="0.20"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Button1"/>

</LinearLayout>

選中列表視圖上的按鈕

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM