Android TwoLineListItem

来源:互联网 发布:物理电子实验室软件 编辑:程序博客网 时间:2024/05/21 09:37

Android TwoLineListItem继承android.widget.RelativeLayout.由两个子视图组合而成,通常用在ListView中。

它需要两个TextView(ID值为text1,text2),还有一个可选的第三个VIew(ID值为selectedIcon)。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xmlversion="1.0"encoding="utf-8"?>
<TwoLineListItemxmlns: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="ssssssssssssssssssssssss"
        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>

效果:

TwoLineListItem

源码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
 
public class TwoLineItemActivity extendsListActivity {
     
    @Override
    publicvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(newContactArrayAdapter(this, R.layout.main,
                getContacts()));
    }
 
    privateList<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);
        returncontacts;
    }
}
</contacts></contacts></contacts>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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 extendsArrayAdapter<contacts> {
     
    privateint resourceId;
 
    publicContactArrayAdapter(Context context, inttextViewResourceId,
            List<contacts> object) {
        super(context, textViewResourceId, object);
        resourceId = textViewResourceId;
        // TODO Auto-generated constructor stub
    }
 
    @Override
    publicView getView(intposition, View convertView, ViewGroup parent) {
        Contacts contacts = getItem(position);
        if(contacts == null) {
            returnnull;
        }
        // 得到一个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());
        }
        returnview;
    }
}
</contacts></contacts>
转自:http://www.android-study.com/jichuzhishi/216.html