我创建了一个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>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
- &GT;
活动Java代码
package 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;
}
}
我仍然无法突出显示和着色各个项目。如果有人能指导我,我会感激不尽。
答案 0 :(得分:3)
您应该创建自己的适配器类和行布局。请看这个问题。
在getView事件中你应该改变你的背景
public class MyCustomAdapter extends SimpleAdapter {
public MyCustomAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
static class ViewHolder {
protected TextView label;
protected Linearlayout layout;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.row_task_layout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.label = (TextView) view.findViewById(R.id.label);
viewHolder.layout= (LineerLayout) view.findViewById(R.id.layout);
view.setTag(viewHolder);
ViewHolder holder = (ViewHolder) view.getTag();
holder.label.setText(data.get(position).title);
if(statement)
{
holder.layout.setBackground(background);
}
return view;
}
}
的RowLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#F3F3F3"
android:gravity="center"
android:padding="5dip" >
<LinearLayout
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="wrap_parent"
android:background="@drawable/button_style_main_single"
android:paddingLeft="5dip"
android:paddingRight="10dip" >
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</LinearLayout>
</LinearLayout>