问题描述
我已经创建了一个机器人列表,我想通过检查一个变量值,得到一个边界颜色个别列表项,并根据该值颜色的背景。这是我的工作至今。
I have created an android list and I want to give a border color to individual list items by checking a variable value and color the background according to the value. This is my work so far .
布局
<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>
@绘制/ border_ui声明
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
- >
-->
活动的Java code
Activity Java Code
包test.application;
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;
}
}
我还有一个不知道以突出和颜色的各个项目。如果任何人都可以指导我,我将感谢。
I still have a no clue to highlight and color the individual items. If anyone can guide me I would be thankful.
推荐答案
您创造前人的精力你自己的接环类和行布局。请看看这个的exaple。
You sould create your own adepter class and a row layout. please look at this exaple.
和在getViev事件前人的精力,你改变你的背景
and in the getViev event you sould change your background
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
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>
这篇关于Android的列表视图,如何给边框和背景颜色android系统中的各个项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!