将按钮添加到android操作栏 [英] adding buttons to android action bar

查看:112
本文介绍了将按钮添加到android操作栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使带有文本的按钮以及带有如下操作栏中的图像的按钮.

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

是否可以使用内置的android按钮或图像按钮执行以下操作栏中的操作.

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

我正在使用appcompat来使用操作栏.

I am using appcompat to use action bar.

推荐答案

是的,您可以根据需要为自定义操作栏充气.

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

其中actionbar_custom.xml是您的布局资源,通常是带有所需组件的LinearLayout.

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

这篇关于将按钮添加到android操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆