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"
?>
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
>
|
效果:
源码:
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>
|