[英]Custom CheckBox menu item ActionBar not work
i have problem with styling Custom CheckBox Menu Item ActionBar. 我在样式自定义复选框菜单项ActionBar时遇到问题。 I have implemented, but It's not styled. 我已经实现了,但是没有样式。
I want to changed with resource this 我想改变资源
if checkbox is checked 如果复选框已选中
if checkbox is not checked 如果未选中复选框
this is my menu item Action Bar.xml 这是我的菜单项Action Bar.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.tracking.bus.maps.SingleViewMapsActivity" >
<item
android:id="@+id/actionbar_alarm"
android:title="@string/actionbar_alarm"
app:actionViewClass="android.widget.CheckBox"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/actionbar_interval"
android:title="@string/actionbar_interval"
app:actionViewClass="android.widget.Spinner"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/actionbar_log_track"
android:title="@string/actionbar_log_track"
app:showAsAction="never"/>
</menu>
style Action Bar 风格的动作栏
<style name="Theme.Myab" parent="@style/Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarItemBackground">@drawable/selectable_background_myab</item>
<item name="popupMenuStyle">@style/PopupMenu.Myab</item>
<item name="dropDownListViewStyle">@style/DropDownListView.Myab</item>
<item name="actionBarTabStyle">@style/ActionBarTabStyle.Myab</item>
<item name="actionDropDownStyle">@style/DropDownNav.Myab</item>
<item name="actionBarStyle">@style/ActionBar.Solid.Myab</item>
<item name="actionModeBackground">@drawable/cab_background_top_myab</item>
<item name="actionModeSplitBackground">@drawable/cab_background_bottom_myab</item>
<item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Myab</item>
<item name="spinnerDropDownItemStyle">@style/DropDownItem.Myab</item>
<item name="android:spinnerItemStyle">@style/SpinnerItem.Myab</item>
<item name="android:checkboxStyle">@style/customCheckBoxStyle.Myab</item>
<item name="actionBarWidgetTheme">@style/Theme.Myab.Widget</item>
</style>
<!-- style checkbox -->
<style name="customCheckBoxStyle.Myab" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/custom_checkbox_design</item>
</style>
custom_checkbox_design.xml custom_checkbox_design.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/alram_on" />
<item android:state_checked="false" android:drawable="@drawable/alram_off" />
</selector>
so how to solve it ? 那么如何解决呢? thanks sorry for my english 谢谢你为我的英语
First things, android:checkboxStyle
should run on device with API 11 or later. 首先, android:checkboxStyle
应该在具有API 11或更高版本的设备上运行。
Try set your checkbox selector like this: 尝试像这样设置您的复选框选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="300">
<item android:state_selected="true" android:drawable="@drawable/checked"></item>
<item android:state_checked="true" android:drawable="@drawable/checked"></item>
<item android:drawable="@drawable/unchecked"></item> <!--default state, unchecked-->
</selector>
I got same issue, I have figured this out tricky way. 我遇到了同样的问题,我想出了这个棘手的方法。 See below: 见下文:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.webview_menu, menu);
menu.getItem(0).setIcon(R.drawable.unfavorite);
favorite = false;
return true; }
On optionitemselected: 在optionitemselected上:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.favoriteItem:
if(favourite){
item.setIcon(R.drawable.unfavorite);
favourite = false;
} else {
item.setIcon(R.drawable.favorite);
favourite = true;
}
new FavoriteTask(favourite).execute();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This was easiest way to implement the same. 这是实现相同目的的最简单方法。
In the java file, you have to set the icon programmatically. 在java文件中,您必须以编程方式设置图标。 If your selector file and menu files are correct, the change of the state of the checkbox will be reflected on the checkbox icon automatically. 如果选择器文件和菜单文件正确,则复选框状态的更改将自动反映在复选框图标上。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu,menu);//insert the name of the menu file
CheckBox checkBox = (CheckBox)menu.findItem(R.id.menu_item).getActionView();//find the checkbox, in your case R.id.actionbar_alarm
checkBox.setButtonDrawable(R.drawable.star);//the name of your selector file custom_checkbox_design.xml in your case
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//add the action of your checkbox
}
});
return super.onCreateOptionsMenu(menu);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.