我创建了一个 android 列表,我想通过检查变量值并根据该值为背景着色,为各个列表项赋予边框颜色。这是我迄今为止的工作。
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/android:myalertlist"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:clickable="true"
android:background="@drawable/border_ui"
android:drawSelectorOnTop="false" >
</ListView>
</RelativeLayout>
我的列表视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txtOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="false" />
<TextView
android:id="@+id/txtTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="false" />
</LinearLayout>
@drawable/border_ui 声明
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
-->
活动 Java 代码
包 test.application;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HashMap<String, String> hashMapOne = new HashMap<String, String>();
hashMapOne.put("KEYONE", "EXPORT");
hashMapOne.put("KEY_TWO", "A");
HashMap<String, String> hashMapTwo = new HashMap<String, String>();
hashMapTwo.put("KEYONE", "IMPORT");
hashMapTwo.put("KEY_TWO", "B");
HashMap<String, String> hashMapThree = new HashMap<String, String>();
hashMapThree.put("KEYONE", "IMPORT");
hashMapThree.put("KEY_TWO", "C");
HashMap<String, String> hashMapFour = new HashMap<String, String>();
hashMapFour.put("KEYONE", "EXPORT");
hashMapFour.put("KEY_TWO", "C");
ArrayList<HashMap<String, String>> hashList = new ArrayList<HashMap<String, String>>();
hashList.add(hashMapOne);
hashList.add(hashMapTwo);
hashList.add(hashMapThree);
hashList.add(hashMapFour);
setContentView(R.layout.activity_main);
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, hashList,
R.layout.list_test, new String[] { "KEYONE",
"KEY_TWO" }, new int[] { R.id.txtOne,
R.id.txtTwo });
final ListView lv = (ListView) findViewById(R.id.android_myalertlist);
lv.setAdapter(simpleAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我仍然不知道如何突出显示单个项目并为其着色。如果有人可以指导我,我将不胜感激。