我已经使用以下布局和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构造函数.见
SimpleAdapter和
ArrayAdapter.
如果要显示一些更多的细节,如图像和文本或两个textview,那么你将必须扩展一个Adapter并实现getView()属性设置图像文本.
如果你想分段列表ListView,你应该去Section ListView in Android检查Section Header in ListView