如何在Android工具栏导航图标中设置复选框?

时间:2016-02-11 17:53:28

标签: java android android-toolbar android-checkbox

我试图在Lollipop图库中设置工具栏的导航图标中的复选框:

enter image description here

我知道你可以改变这样的图像:

mToolbar.setNavigationIcon(iconRes);

但是如何将导航图标更改为复选框?我尝试使用自定义布局,但没有奏效:

checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true" />

</RelativeLayout>

并在活动中:

mToolbar.setNavigationIcon(R.layout.checkbox);

但这不起作用。

2 个答案:

答案 0 :(得分:1)

您只能使用setNavigationIcon的可绘制资源ID。你不能在那里放置布局。

答案 1 :(得分:0)

我删除了原始导航图标和工具栏标题,用toolbar.h替换了我自己的复选框和标题。