【问题标题】:menu Item not showing if actionLayout is set如果设置了 actionLayout,菜单项不显示
【发布时间】:2018-04-02 18:33:08
【问题描述】:

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

问题是,每次我设置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 答案不是正确的解决方案

【问题讨论】:

    标签: android android-actionbar badge


    【解决方案1】:

    由于您已经在 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>
    

    【讨论】:

    • 现在可以了,谢谢!顺便说一句,为什么 android:icon 属性不起作用?
    • 为什么当我点击我的同步项目时没有调用onOptionsItemSelected
    • 删除android:icon导致menuItem.icon为空,有什么解决办法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 2014-09-18
    相关资源
    最近更新 更多