如果设置了 actionLayout,则菜单项不显示

menu Item not showing if actionLayout is set

我正在尝试为我的 actionBar 上的项目使用 "badge",类似这样但没有数字:

问题是,每次我设置 app:actionLayout="@layout/actionbar_badge_layout" 时,我的同步项目都没有显示(但徽章显示)。我正在使用此代码:

badge_noty.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">

    <solid android:color="@color/colorRed"/>
    <stroke android:color="@android:color/white" android:width="1dp"/>

</shape>

actionbar_sync.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/action_sync"
        android:icon="@drawable/ic_sync"
        android:title="Sync"
        app:actionLayout="@layout/actionbar_badge_layout"
        app:showAsAction="always"
   />
</menu>

actionbar_badge_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="?attr/actionButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clipToPadding="false"
    android:focusable="true"
    >

    <!-- Badge -->
    <ImageView
        android:id="@+id/img_badge_sync"
        android:layout_width="12dp"
        android:layout_height="12dp"
        android:src="@drawable/badge_noty" />

</FrameLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private ImageView syncBadge;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ...............
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.actionbar_sync, menu);

        final MenuItem syncItem = menu.findItem(R.id.action_sync);
        View actionView = syncItem.getActionView();
        syncBadge = actionView.findViewById(R.id.img_badge_sync);

        return true;
    }

为什么我的同步图标没有显示?

编辑 1: 我尝试了 Imtiaz Abir 答案,它起作用了,但没有达到预期效果。现在,当我单击同步项时,不会触发 onOptionsItemSelected。所以,我认为 Imtiaz Abir 答案不是正确的解决方案

因为您已经在 actionbar_sync.xml 菜单文件的菜单项中定义了 app:actionLayout,默认 android:icon 属性将不起作用。因此,解决方案是在 actionbar_badge_layout.xml 文件中添加使用 ImageView 的同步图标以及带徽章的 ImageView 并删除 android:菜单项中的 icon 属性。

actionbar_sync.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/action_sync"
        android:title="Sync"
        app:actionLayout="@layout/actionbar_badge_layout"
        app:showAsAction="always"
   />
</menu>

actionbar_sync.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="?attr/actionButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clipToPadding="false"
    android:focusable="true"
    >

    <!-- Badge -->
    <ImageView
        android:id="@+id/img_badge_sync"
        android:layout_width="12dp"
        android:layout_height="12dp"
        android:src="@drawable/badge_noty" />

     <!-- Sync image -->
     <ImageView
        android:id="@+id/icon_sync"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:src="@drawable/ic_sync" />

</FrameLayout>