Item title not displayed in portrait with SHOW_AS_ACTION_WITH_TEXT

浪尽此生 提交于 2019-12-23 15:34:46

问题


In my activity, I have an action mode with a single item which has a title and an icon.

I want both the title and the icon to be displayed, so I use SHOW_AS_ACTION_WITH_TEXT and SHOW_AS_ACTION_ALWAYS flags.

In landscape orientation, it's fine. I have title + icon. But in portrait orientation only the icon is displayed (though there's a lot of free space). Does anyone know what I could do to fix it ?

Note that the title is correctly displayed if I remove the icon.

Here is my sample code:

public class TestActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    startActionMode(new Callback() {

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            menu.add("Item 1").setIcon(R.drawable.ic_launcher).setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT | MenuItem.SHOW_AS_ACTION_ALWAYS);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; }

        @Override
        public void onDestroyActionMode(ActionMode mode) { }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) { return false; }
    });
}
}

回答1:


Does anyone know what I could do to fix it ?

I doubt that you can. "Always" and "with text" are requests, not commands. The framework does not always honor either of them.




回答2:


Instead of adding this menu item via code try to use this attribute:

android:showAsAction="always|withText"

and add it to the item in the menu.xml



来源:https://stackoverflow.com/questions/13418068/item-title-not-displayed-in-portrait-with-show-as-action-with-text

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