可能还有其他方法,但是我会在按钮的OnClick方法中遍历列表行。喜欢的东西:
在您的活动字段定义:
static final int colourA=Color.argb(255,255,0,0);
static final int colourB=Color.argb(255,0,255,0);
int currentColour=colourA;
在您的活动的OnCreate:
Button myButton = (Button) findViewById(R.id.myButton);
final ListView myListView = (ListView) findViewByID(R.id.myListView);
//change myButton to your button id, and myListView to your ListView id
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//This is the code to toggle the colours, you can do pretty much whatever you want here though
if (currentColour==colourA){
currentColour=colourB;
} else {
currentColour=colourA;
}
//This cycles through all the root views in the ListView. If you want to change the
//colour of only one view in the row layout, in the for loop use
//rowView.findViewById(R.id.myViewInRow).setBackgroundColor(currentColour);
//instead, to get the relevant view in the row
View rowView;
for (int i=0;i<myListView.getChildCount();i++){
rowView=myListView.getChildAt(i);
rowView.setBackgroundColor(currentColour);
}
}
});
是否要更改所有textview背景颜色? –
@ChiragRaval ...是 – sarath