Remove Title from Contextual Action Bar

天大地大妈咪最大 提交于 2019-12-04 16:58:05

Try calling setCustomView(null) on the ActionMode.

If that does not work, try setTitleOptionalHint(true) on the ActionMode.

If that does not work, try setTitle("") on the ActionMode.

Also note that I do not want to set android:showAsAction="always"

You have to if you want more than two action buttons in the ActionBar, at least on smaller screens. The ActionBarPolicy determines how many action button to place in the ActionBar and the default amount is 2.

The only way to override that default value is to make your MenuItem "always" appear.

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        mode.setTitle(null);

        for (int i = 0; i < 5; i++) {
            menu.add("Item " + (i + 1)).setIcon(android.R.drawable.sym_def_app_icon)
                    .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        }
        return true;
    }

Results

sodesuga nandemoi

Following adneal's answer I ran into problem how to deal with the action. so I found this solution simpler: still using the xml/inflater but add

 menu.findItem(R.id.whatever).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
@Ovrride
protected void onCreate(Bundle savedInstanceState……

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.prva.xml);

I tried the solutions provided here and made some observations:

  • The view for the title is always contained in the view-hierachy (-> Layout Inspector from Android Studio) But it is in the background and will be overridden by the content.
  • setContentView adds the view provided to the layuot (in the foreground). So setContentView(null) does not remove the view for the title. (Still visible in the Layout Inspector)
  • app:showAsAction="always" is overridden when inflating the layout. You can set 3 items to be always displayed but only 2 items will be displayed.

But setting .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS) programmatically after inflating the menu works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!