【问题标题】:how to add the cutom row to the ListView in android如何将自定义行添加到android中的ListView
【发布时间】:2013-09-10 18:27:35
【问题描述】:

我正在开发一个简单的 android 应用程序,它有一个自定义列表视图,其中我有一个图像,并且在该图像名称和描述旁边。我在我的布局文件夹中创建了一个 custom_list.xml 并有一个图片列表适配器,它将数据添加到imageview,textview,textview。这是我的 custom_list.xml 代码。

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


<LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="5dip"
    android:padding="3dip" >
    <ImageView
        android:id="@+id/list_image"
        android:contentDescription="@string/app_name"
        android:layout_width="60dip"
        android:layout_height="60dip"
        android:src="@android:drawable/ic_menu_gallery"
         />
</LinearLayout>

<ImageView
    android:id="@+id/next"
    android:contentDescription="@string/app_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:src="@drawable/next" />

<TextView
    android:id="@+id/desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageName"
    android:layout_alignLeft="@+id/imageName"
    android:paddingTop="5dip"
    android:layout_centerHorizontal="true"
    android:text="@string/app_name"
    android:textColor="#343434"
    android:textSize="15sp" />

<TextView
    android:id="@+id/imageName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/next"
    android:layout_marginLeft="30dp"
    android:layout_toRightOf="@+id/thumbnail"
    android:paddingBottom="10dip"
    android:text="@string/home"
    android:textColor="#040404"
    android:textSize="25sp"
    android:textStyle="bold"
    android:typeface="sans" />

</RelativeLayout>  

我能够加载数据并且列表很好,我的问题是需要在这个列表中显示固定的页眉和页脚,我采取了一个单独的活动(MyActivity)并在 MyCustomActivity.xml 中编写了以下代码来添加像这样的页眉和页脚

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"

tools:context=".MyCustomActivity" >

  <include layout="@layout/header"
    android:id="@+id/hd"/>
  <include layout="@layout/activity_custom_list"
      android:id="@+id/listView"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"  />
  <include
    android:id="@+id/ft"
    layout="@layout/footer" />

</LinearLayout>

但是页眉和页脚还是没有进入屏幕,屏幕看起来和以前一样。谁能给我一个如何为我的自定义列表视图添加页眉和页脚的方法,我还有另一个疑问,我是否需要放置一个 ListView 标签来代替中间包含标签,如果是,那么如何将列表添加到 ListView标签。 请多多包涵,我对android很陌生,谢谢

【问题讨论】:

  • 在您的 BaseAdapter 中,您可以测试单元格编号。如果它是 0 或最后一个,您只需为该行使用另一个布局/对象
  • 您的MyActivity 是从什么扩展而来的?
  • @sunilMishra 它的 MyCustomActivity 不是 MyActivity(打字错误)它从 ListActivity 扩展而来,感谢您的早期回复
  • ListActivity 更改为Activity
  • @sunilMishra 如果我将其更改为仅活动标题,一个默认列表,页脚即将到来,因为我无法调用我的 listAdapter(setListAdapter(new PhotoViewerCustomAdapter(parentAct, imageDetails)))..你看到我的点。

标签: android android-listview header footer


【解决方案1】:

将 layout_weight 属性添加到 ListView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"

tools:context=".MyCustomActivity" >

  <include layout="@layout/header"
    android:id="@+id/hd"/>
  <include layout="@layout/activity_custom_list"
      android:id="@+id/listView"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
      android:layout_weight="1" />
  <include
    android:id="@+id/ft"
    layout="@layout/footer" />

</LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多