如何以编程方式更改radiobutton
popupmenu
颜色
private void showPopupMenu(View view) {
PopupMenu popupMenu = new PopupMenu(getContext(), view);
popupMenu.getMenuInflater().inflate(R.menu.menu_publish_more_popup, popupMenu.getMenu());
MenuItem postItem = popupMenu.getMenu().findItem(R.id.action_post);
postItem.setChecked(true);
RadioButton radioButton = (RadioButton) (postItem.getActionView());
if (Build.VERSION.SDK_INT >= 21) {
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_enabled}, //disabled
new int[]{android.R.attr.state_enabled} //enabled
},
new int[]{
Color.GREEN
, Color.RED
}
);
radioButton.setButtonTintList(colorStateList);//set the color tint list
radioButton.invalidate(); //could not be necessary
}
popupMenu.show();
}
答案 0 :(得分:0)
首先定义colorstatelist
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_enabled} //enabled
},
new int[] {getResources().getColor(R.color.colorPrimary) }
);
然后分配
AppCompatRadioButton radioButton = (AppCompatRadioButton) findViewById(R.id.rb);
radioButton.setSupportButtonTintList(colorStateList);
AppCompatCheckBox cbSelected = (AppCompatCheckBox) findViewById(R.id.cbSelected);
cbSelected.setSupportButtonTintList(colorStateList);
或者如果这不起作用,那么使用它:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
male.setButtonTintList(ColorStateList.valueOf(ContextCompat.getColor(ReservationContact.this, R.color.background)));
}
male.setHighlightColor(getResources().getColor(R.color.background));
答案 1 :(得分:0)
试试这段代码:
PopupMenu popupMenu = new PopupMenu(this, view);
popupMenu.getMenuInflater().inflate(R.menu.menu_publish_more_popup, popupMenu.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
if(item.getTitle().equals("Create Post")){
if(Build.VERSION.SDK_INT>=21)
{
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_enabled}, //disabled
new int[]{android.R.attr.state_enabled} //enabled
},
new int[] {
Color.WHITE//disabled
,Color.RED//enabled
}
);
radio.setButtonTintList(colorStateList);//set the color tint list
radio.invalidate(); //could not be necessary
}
}
return true;
}
});
popup.show();//showing popup menu
答案 2 :(得分:0)
你可以尝试,
MenuItem menuitem=popupMenu.getMenu().findItem(R.id.action_post);
RadioButton radioButton= (RadioButton)(MenuItemCompat.getActionView(menuitem));//menuitem.getActionView();
radioButton.setButtonTintList(colorstateList);
在您的菜单中,添加actionViewClass
,
<item
android:id="@+id/action_post"
android:actionViewClass="android.widget.RadioButton"
/>
您的colorStateList
应该是,
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{}, //otherwise for all cases
new int[]{android.R.attr.state_checked} //checked
},
new int[]{
Color.GREEN,Color.RED
}
);