如何在 Android ListView 中添加子项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20320794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a sub item in Android ListView?
提问by CrazyLearner
I have made a android listView taking the help from Vogella.comusing following layout and ListActivity class.
我使用以下布局和 ListActivity 类在Vogella.com的帮助下制作了一个 android listView 。
RowLayout.xml
行布局.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
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();
}
}
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?
我想在 textView 下方添加一个子项,并将全文部分保留在每行的中心。我该怎么做?
采纳答案by GrIsHu
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 SimpleAdapterand ArrayAdapter.
一个ListView
项目可以有它自己的自定义布局。当您为您创建适配器时,ListView
您可以将布局 ID 传递给 Adapter 构造函数。请参阅SimpleAdapter和ArrayAdapter。
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.
如果你想显示更多的细节,比如图像和文本或两个文本视图,那么你将不得不扩展一个适配器并实现getView()
属性设置图像+文本。
Check out Custom ListView
查看自定义列表视图
And if you want to categorize the ListView in sections then you should go for the Section ListView in Androidalso check Section Header in ListView
如果你想在部分中对 ListView 进行分类,那么你应该去Android 中的部分 ListView也检查ListView 中的部分标题
回答by Thein
You can use the built-in android.R.layout.simple_list_item_2 to create two line textView.
您可以使用内置的 android.R.layout.simple_list_item_2 创建两行 textView。