gpt4 book ai didi

java - 操作栏自定义组件(自定义开关)未随 actionLayout 显示

转载 作者:行者123 更新时间:2023-12-01 12:31:33 25 4
gpt4 key购买 nike

我想在操作栏中创建自定义开关。这是我的菜单 xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:autolux="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/actionbar_notification_switch"
android:title=""
android:actionLayout="@layout/notification_switch"
autolux:showAsAction="always"/>
</menu>

这是我的自定义通知开关 xml notification_switch.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/color"
android:textOff="OFF"
android:textOn="ON"
android:thumb="@drawable/navigation_switch_selector"
android:checked="false"
android:id="@+id/actionbar_notification" />

</RelativeLayout>

这就是我从我的 Activity 中调用的:

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.notification_menu, menu);
return true;
}

但是我的操作栏不显示我的自定义开关(或任何其他组件,如果我将其放入 notification_switch.xml 中)。我还可以将 main.xml 菜单设置为我的菜单,该菜单仅使用图标并且有效。该代码是:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:autolux="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_bar_filter"
android:icon="@drawable/ic_sort"
android:title="@string/action_settings"
autolux:showAsAction="always" />

</menu>

我似乎无法弄清楚我的自定义组件出了什么问题。任何帮助将不胜感激。 (附加信息是:Min Sdk Version 14、Compile Sdk Version 19、Build Tools Version 20

最佳答案

希望对您有所帮助:

删除:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.notification_menu, menu);
return true;
}

然后在您的onCreate方法中:

 ActionBar mActionBar = getActionBar();
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.notification_switch, null);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);

引用:

http://javatechig.com/android/actionbar-with-custom-view-example-in-android

如果您使用actionBarCompat,请使用getSupportActionBar而不是getActionBar

关于java - 操作栏自定义组件(自定义开关)未随 actionLayout 显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25894485/

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