Android TwoLineListItem

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
<? 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 = "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 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;
     }
}
</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 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;
     }
}
</contacts></contacts>
转自:http://www.android-study.com/jichuzhishi/216.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值