TwoLineListItem继承android.widget.RelativeLayout.由两个子视图组合而成,通常用在ListView中。
它需要两个TextView(ID值为text1,text2),还有一个可选的第三个VIew(ID值为selectedIcon)
<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/presence_offline"/>
<TextView android:id="@android:id/text1"
android:layout_marginTop="1dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/icon"
android:textSize="15dip"
android:text="ssssssssssssssssssssssssss"
android:textStyle="bold" />
<TextView android:id="@android:id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@android:id/text1"
android:layout_alignLeft="@android:id/text1"
android:paddingBottom="4dip"
android:includeFontPadding="false"
android:textSize="15dip"
android:text=""
android:textStyle="normal" />
<ImageView android:id="@android:id/selectedIcon"
android:layout_marginTop="9dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="7dip"
android:src="@android:drawable/sym_action_call"/>
</TwoLineListItem>
效果:
源码:
import java.util.ArrayList;import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
public class TwoLineItemActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ContactArrayAdapter(this, R.layout.main, getContacts()));
}
private List<Contacts> getContacts() {
List<Contacts> contacts = new ArrayList<Contacts>();
Contacts c;
c = new Contacts();
c.setName("Shriram");
c.setPhone("123456");
contacts.add(c);
c = new Contacts();
c.setName("John");
c.setPhone("456789");
contacts.add(c);
return contacts;
}
}
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TwoLineListItem;
public class ContactArrayAdapter extends ArrayAdapter<Contacts> {
private int resourceId;
public ContactArrayAdapter(Context context, int textViewResourceId,List<Contacts>object) {
super(context, textViewResourceId,object);
resourceId = textViewResourceId;
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position,View convertView,ViewGroup parent){
Contacts contacts = getItem(position);
if(contacts==null){
return null;
}
//得到一个LayoutInflater实例
LayoutInflater inflater =(LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TwoLineListItem view;
if(convertView==null){
view=(TwoLineListItem) inflater.inflate(resourceId, parent,false);//由xml生成View
}else{
view = (TwoLineListItem) convertView;
}
if(view.getText1()!=null){
view.getText1().setText(contacts.getName());
}
if(view.getText2()!=null){
view.getText2().setText(contacts.getPhone());
}
return view;
}
}