gpt4 book ai didi

android - 在工具栏的自定义布局中找不到切换兼容性的引用

转载 作者:行者123 更新时间:2023-11-30 02:17:47 26 4
gpt4 key购买 nike

我正在尝试将 switchcompat 添加到工具栏,菜单项引用自定义布局

toolbar_switch.xml

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

<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
app:showText="false"
android:id="@+id/switch_online"
android:layout_centerInParent="true"
android:layout_height="wrap_content" />

</RelativeLayout>

menu_main_activity.xml

<?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_toggle"
android:actionLayout="@layout/toolbar_switch"
app:showAsAction="always"
android:title="" />
</menu>

如果我将菜单项设置为图像或文本,菜单会在 Activity 中膨胀,但当我将其指向自定义布局时,它不会显示

    setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setOnMenuItemClickListener(this);

switchOnline = (SwitchCompat)toolbar.findViewById(R.id.switch_online);

我的应用主题是基于 AppCompat

最佳答案

由于您使用的是支持操作栏,因此您需要使用 app:actionLayout 来指定您的自定义 View 。

关于android - 在工具栏的自定义布局中找不到切换兼容性的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29021466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com