如何在Android的ListView中添加一个子项? [英] How to add a sub item in Android ListView?

查看:120
本文介绍了如何在Android的ListView中添加一个子项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了的Andr​​oid的ListView采取的帮助 Vogella.com 使用以下布局和ListActivity类。

I have made a android listView taking the help from Vogella.com using following layout and ListActivity class.

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的下方的子项,并保持完整的文本部分中的各行的中心。我该怎么办呢?

I want to add a sub item below the textView and keep the full text portion in the center of each row. How can I do it?

推荐答案

A 的ListView 项目可以有它自己的自定义布局。当你创建你的的ListView适配器您可以在布局ID的适配器构造通过。请参见 SimpleAdapter 并的 ArrayAdapter

A ListView item can have it's own custom layout. When you create your adapter for the ListView you can pass in the layout id to the Adapter constructor. See SimpleAdapter and ArrayAdapter.

如果你想显示象图片和文字两个TextView的一些细节,然后你将不得不延长了适配器和实施 getView() 财产设定图像+文字。

If you want to show some more details like image and text or two textview then You will have to extend an Adapter and implement getView() to property set the image+text.

查看自定义的ListView

如果你想在分类部分ListView控件,那么你应该去在Android中科的ListView也检查节头ListView中

And if you want to categorize the ListView in sections then you should go for the Section ListView in Android also check Section Header in ListView

这篇关于如何在Android的ListView中添加一个子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆