我有样式自定义CheckBox菜单项ActionBar的问题。我已经实现了,但它没有风格。
我想用资源改变
如果选中复选框
如果未选中复选框
这是我的菜单项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 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
<?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>
那怎么解决呢?谢谢对不起我的英文
答案 0 :(得分:2)
首先,android:checkboxStyle
应该在使用API 11或更高版本的设备上运行。
尝试设置您的复选框选择器:
<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>
答案 1 :(得分:2)
我遇到了同样的问题,我认为这很棘手。见下文:
@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; }
关于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);
}
}
这是实现相同目标的最简单方法。
答案 2 :(得分:0)
在java文件中,您必须以编程方式设置图标。如果选择器文件和菜单文件正确,则复选框状态的更改将自动反映在复选框图标上。
@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);
}