我自己一直在尝试太多时间,并且无法在列表视图中获取行的背景颜色。
这是我的代码:
public class InteractiveArrayAdapter extends ArrayAdapter<Object> {
private final List<Object> list;
private final Activity context;
public InteractiveArrayAdapter(Activity context, List<Object> list) {
super(context, R.layout.rowbuttonlayout, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.rowbuttonlayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
Object obj = list.get(position);
//if we have a String object it's a different style
if (obj.getClass() == String.class) {
holder.text.setText((String) obj);
holder.checkbox.setVisibility(View.GONE);
/* tried this - does not work
view.setBackgroundColor(0xAAAA);
*/
holder.text.setTextColor(Color.BLUE); // this is OK
holder.text.setTextSize((float) 18.0); // this is OK
} else { // it's a different row style
holder.text.setText(x.name);
holder.checkbox.setVisibility(View.VISIBLE); // this is OK
holder.text.setTextColor(Color.BLACK); // this is OK
holder.text.setTextSize((float) 15.0); // this is OK
}
return view;
}
}
这是rowbuttonlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="@+id/label"
android:textSize="12dp" >
</TextView>
</RelativeLayout>
无论我试图改变行颜色 - 它都会保持黑色。唯一有效的方法是引入一个ImageView。但我认为这只是为了改变一行的颜色。
此外,我没有完全用图像视图填充行。
所以我所寻找的是一个解决方案,可以设置行颜色,就像其他样式属性一样 - 顺便说一句,它可以正常工作。
谢谢!
答案 0 :(得分:0)
如果要更改要单击的行的颜色,请在ListView活动中或使用ListView尝试此操作。
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
// TODO Auto-generated method stub
view.setBackgroundColor(Color.CYAN);
}
或
ListView.onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
// TODO Auto-generated method stub
view.setBackgroundColor(Color.CYAN);
}
希望这有帮助。
答案 1 :(得分:0)
我可能不明白,但这不起作用:
view.setBackgroundColor(0x222222);