如何在Android ListView中添加子项?

我已经使用以下布局和ListActivity类从 Vogella.com获取了android listView.

RowLayout.xml

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

    <ImageView
        android:id="@+id/icon"
        android:layout_width="22px"
        android:layout_height="22px"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px"
        android:layout_marginTop="4px"
        android:src="@drawable/icon" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20px" >
    </TextView>

</LinearLayout>

MyListActivity.java

package de.vogella.android.listactivity;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MyListActivity extends ListActivity {
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    String[] values = new String[] { "Android","iPhone","WindowsMobile","BlackBerry","WebOS","Ubuntu","Windows7","Max OS X","Linux","OS/2" };
    // use your own layout
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.rowlayout,R.id.label,values);
    setlistadapter(adapter);
  }

  @Override
  protected void onListItemClick(ListView l,View v,int position,long id) {
    String item = (String) getlistadapter().getItem(position);
    Toast.makeText(this,item + " selected",Toast.LENGTH_LONG).show();
  }
}

我想在textView下面添加一个子项,并将全文部分保留在每行的中心.我该怎么做?

解决方法

ListView项可以拥有自己的自定义布局.为ListView创建适配器时,可以将布局ID传递给Adapter构造函数.见 SimpleAdapterArrayAdapter.

如果要显示一些更多的细节,如图像和文本或两个textview,那么你将必须扩展一个Adapter并实现getView()属性设置图像文本.

查看Custom ListView

如果你想分段列表ListView,你应该去Section ListView in Android检查Section Header in ListView

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...