Remove Title from Contextual Action Bar

孤街醉人 提交于 2019-12-06 10:15:50

问题


I want to remove the view that is allocated for the title in a contextual action bar.

Please note that I do not want to set the title; I want the View entirely gone. This is to allow more room so that actions can be displayed on the menu bar rather than being dropped into the overflow menu.

Also note that I do not want to set android:showAsAction="always". I want Android to decide how much space there is for the icons. I just want there to be more space by removing the blank area that is reserved for a title.

UPDATE

Things I have tried:

  • ActionMode.setCustomView(null)
  • ActionMode.setTitleOptionalHint(true)
  • ActionMode.setTitle("").

Unfortunately, none of these have worked.


UPDATE 2

It turns out my desired results are impossible to achieve in the way I was attempting. Removing the title will not allow room for more icons; Android sets a hard limit based upon the device screen size. If you are wanting to add more icons to the menu, see the answer provided by @adneal.

If you actually have a title (as shown below) and want to remove it, then you can call ActionMode.setTitle(""), as provided by @CommonsWare in the accepted answer.


回答1:


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.




回答2:


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




回答3:


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);



回答4:


@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);



回答5:


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.



来源:https://stackoverflow.com/questions/23253475/remove-title-from-contextual-action-bar

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