如何在Android的ListView中添加一个子项?

编程入门 行业动态 更新时间:2024-10-21 16:04:15
本文介绍了如何在Android的ListView中添加一个子项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

RowLayout.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="schemas.android/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中添加一个子项?

本文发布于:2023-11-23 15:56:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622037.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:个子   如何在   Android   ListView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!