如何在具有自定义背景的工具栏中添加复选框-Android [英] How to Add a CheckBox in Toolbar with custom background - android
本文介绍了如何在具有自定义背景的工具栏中添加复选框-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是android新手,
i想在我的工具栏中有一个带有自定义背景的复选框
我的用例是:我想将当前帖子(在我的活动中)添加到收藏夹通过工具栏
中的复选框列出,我想使用星形(开/关)图标。
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.
我尝试了此操作,但checkBox为null
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" />
和我的活动:
@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");
}
推荐答案
使用工具栏可以执行此操作
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
这篇关于如何在具有自定义背景的工具栏中添加复选框-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文