我正在尝试将某些 ListView 项目(如电话号码和电子邮件地址)的颜色单独设置为蓝色,以便用户知道它们是可点击的。这是一个片段,但实现这一点根本无法正常工作,当您滚动列表时,列表项会随机改变颜色。
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
//...add data to list
SimpleAdapter adapter=new SimpleAdapter(this, data, R.layout.simple_list_item_2_custom, new String[] { LABEL, VALUE },
new int[] { R.id.text1, R.id.text2 })
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LinearLayout view = (LinearLayout)super.getView(position, convertView, parent);
String label = data.get(position).get(LABEL);
if (label.equals("Email Address"))
{
((TextView)view.getChildAt(0)).setTextColor(Color.BLUE);
}
return view;
}
};
这是定义列表项的 LinearLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="vertical" >
<TextView android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="?android:attr/listPreferredItemPaddingStart"
android:layout_marginTop="8dip"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignStart="@android:id/text1"
android:textAppearance="?android:attr/textAppearanceListItem" />
</LinearLayout>