How to display menu item with icon and text in AppCompatActivity

前端 未结 9 1843
遥遥无期
遥遥无期 2020-11-29 05:11

I tried different combinations in xml file:



        
9条回答
  •  情书的邮戳
    2020-11-29 05:55

    Adding to the answer from dev_ry, there is a much smoother way without using reflection by just casting and suppressing the restricted API warning:

    import android.support.v7.view.menu.MenuBuilder;
    
    @SuppressLint("RestrictedApi")
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (menu instanceof MenuBuilder) {
            ((MenuBuilder) menu).setOptionalIconsVisible(true);
        }
        getMenuInflater().inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
    

提交回复
热议问题