问题
Hi i'm new in android and here i want to Have a CheckBox in my Toolbar with a custom background my use case is : i want to add current post(in my activity) to favorites list by a checkbox in toolbar and i wanna use a star(on/off) icon.
i tried this but the checkBox is null
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/shwo_menu_download_mp3"
android:title="@string/download_mp3"
app:showAsAction="never"/>
<item android:id="@+id/show_menu_add_to_fav"
android:checked="true"
android:enabled="true"
app:showAsAction="always"
/>
<item android:id="@+id/show_menu_setting"
android:title="@string/show_menu_setting"
app:showAsAction="never" />
and my Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_speech);
initCheckBox();
...
}
CheckBox checkBoxFav;
private void initCheckBox() {
checkBoxFav = (CheckBox) findViewById(R.id.show_menu_add_to_fav);
checkBoxFav.setText("some Text");
}
回答1:
with toolbar you can do this
setSupportActionBar(toolbar);
View view= getLayoutInflater().inflate(R.layout.view_, null);
Checkbox chbox = view.findViewById(..);
//chbox.do what u want with it
toolbar.addView(logo);
the view layout contains a checkbox and design it as you wish
回答2:
You have to follow these steps:
1. Create a toolbar and add it to your activity. I'm not going to explain that since there are a ton of tutorials on the matter.
2. Create a menu with a checkbox widget in it:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="@+id/menu_star"
android:checkable="true"
app:actionViewClass="android.widget.CheckBox"
app:showAsAction="ifRoom"
android:title="@string/favorite" />
</menu>
Add the set of icons in the drawable folder. One icon for the unchecked state and one icon for the checked state.
Create a resource file and name it whatever you want (In this example the name is
star.xml), add it to the drawable folder and insert the following code in it:<?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/star_black" /> <item android:state_selected="true" android:drawable="@drawable/star_black" /> <item android:state_checked="false" android:drawable="@drawable/star_border_black" /> <item android:drawable="@drawable/star_border_black" /> </selector>create an
onCreateOptionsMenumethod in your activity and insert the following code. The icon of the checkbox will change automatically after getting checked or unchecked.@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.menu,menu); CheckBox checkBox = (CheckBox)menu.findItem(R.id.menu_star).getActionView(); checkBox.setButtonDrawable(R.drawable.star);//set the icon to star.xml checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { //set the action of the checkbox } }); return super.onCreateOptionsMenu(menu); }
来源:https://stackoverflow.com/questions/43248146/how-to-add-a-checkbox-in-toolbar-with-custom-background-android