adding buttons to android action bar

喜欢而已 提交于 2020-01-01 03:18:05

问题


How can i make the buttons with text,with images in action bar like below.

Is it possible to use Inbuilt android button or image button to do like below in action bar.

I am using appcompat to use action bar.


回答1:


Yes, you can inflate a custom actionbar if you need.

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

    LayoutInflater inflater = (LayoutInflater) getActionBar()
            .getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    View customActionBarView = inflater.inflate(R.layout.actionbar_custom, null);

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayOptions(
            ActionBar.DISPLAY_SHOW_CUSTOM,
            ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME
                    | ActionBar.DISPLAY_SHOW_TITLE);
    actionBar.setCustomView(customActionBarView,
            new ActionBar.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));

    setContentView(R.layout.activity_main);
}

Where actionbar_custom.xml would be your layout resource, usually a LinearLayout with whatever components you want.




回答2:


You mean action items?http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems

You need to create a menu layout, as the guide mentioned above describes.

The default icon set for Android could be found here: http://developer.android.com/design/downloads/index.html



来源:https://stackoverflow.com/questions/19346434/adding-buttons-to-android-action-bar

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