【发布时间】:2016-02-16 06:28:17
【问题描述】:
我有代码,如果点击哪一个,我的 textview 颜色会改变,但它只会改变 1 行.. 我想改变所有列表项的颜色 enter image description here
这是我的截图 这是我的代码 我不知道这段代码有什么问题,
PS:即使我没有按下按钮,有时如果我滚动列表视图,颜色会自行改变
public void colorToggle(View view) {
int[] attrs = {android.R.attr.popupBackground};
TypedArray ta = obtainStyledAttributes(R.style.MyApp_PopupMenu, attrs);
final LinearLayout propLayout = (LinearLayout) findViewById(R.id.leot);
ListView listView = (ListView) findViewById(android.R.id.list);
TextView textView = (TextView) findViewById(R.id.wilayah);
switch (view.getId()) {
case R.id.blueButton: {
int holoBlue = getResources().getColor(R.color.holo_blue_light);
mFab.setColor(holoBlue);
getActionBar().setBackgroundDrawable(new ColorDrawable(holoBlue));
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
int popupBackground = ta.getColor(0, R.color.holo_blue_light);
Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
propLayout.setVisibility(View.GONE);
listView.setDivider(new ColorDrawable(holoBlue));
listView.setDividerHeight(1);
textView.setTextColor(holoBlue);
break;
}
case R.id.purpleButton: {
int holoPurple = getResources().getColor(R.color.holo_purple);
mFab.setColor(holoPurple);
getActionBar().setBackgroundDrawable(new ColorDrawable(holoPurple));
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
int popupBackground = ta.getColor(0, R.color.holo_purple);
Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
propLayout.setVisibility(View.GONE);
listView.setDivider(new ColorDrawable(holoPurple));
listView.setDividerHeight(1);
textView.setTextColor(holoPurple);
break;
}
case R.id.greenButton: {
int holoGreen = getResources().getColor(R.color.holo_green_light);
mFab.setColor(holoGreen);
getActionBar().setBackgroundDrawable(new ColorDrawable(holoGreen));
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
int popupBackground = ta.getColor(0, R.color.holo_green_light);
Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
propLayout.setVisibility(View.GONE);
listView.setDivider(new ColorDrawable(holoGreen));
listView.setDividerHeight(1);
textView.setTextColor(holoGreen);
break;
}
case R.id.orangeButton: {
int holoOrange = getResources().getColor(R.color.holo_orange_light);
mFab.setColor(holoOrange);
getActionBar().setBackgroundDrawable(new ColorDrawable(holoOrange));
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
int popupBackground = ta.getColor(0, R.color.holo_orange_light);
Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
propLayout.setVisibility(View.GONE);
listView.setDivider(new ColorDrawable(holoOrange));
listView.setDividerHeight(1);
textView.setTextColor(holoOrange);
break;
}
case R.id.redButton: {
int holoRed = getResources().getColor(R.color.holo_red_light);
mFab.setColor(holoRed);
getActionBar().setBackgroundDrawable(new ColorDrawable(holoRed));
mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
int popupBackground = ta.getColor(0, R.color.holo_red_light);
Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
propLayout.setVisibility(View.GONE);
listView.setDivider(new ColorDrawable(holoRed));
listView.setDividerHeight(1);
textView.setTextColor(holoRed);
break;
}
}
ta.recycle();
}
【问题讨论】: