我创建了一个动态视图(线性布局)
table.addView(view, layoutParams);
我希望当我点击 view1 时背景颜色变为白色,当我点击 view2 时,view1 的背景颜色变为黑色,view2 的背景颜色变为白色,如果我点击 view3,view2 的背景颜色变为黑色和背景view3 的颜色变成白色...
private void addToRow(int rowID, TimeCard timecard, int offset) {
LayoutParams tlparams = new LinearLayout.LayoutParams(TIMECARD_WIDTH,
LinearLayout.LayoutParams.WRAP_CONTENT);
tlparams.setMargins((int) offset, 150-50*rowID, 0, 0);
System.out.println("rowID= "+50*rowID);
View v = timecard.getView(context);
v.setOnClickListener(this);
rows.get(rowID).addView(v, tlparams);
}
private void addRowsToTable() {
LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (LinearLayout row : rows) {
layoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//table is an absolutlayout and i add many row in this layout
table.addView(row, layoutParams);
}
tableNavbar.addView(llnavBar, layoutParams);
}
@Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.cadre_blanc);
TextView t1=(TextView)v.findViewById(R.id.tc_title);
t1.setTextColor(Color.rgb(0, 139, 221));
}
请问我该怎么做??