【问题标题】:How can I customize the ActionBar如何自定义 ActionBar
【发布时间】:2015-11-23 07:05:07
【问题描述】:

如何将 ActionBar 图标设置在左右并将其标题设置为中心?
我现在可以将标题文本放在它的中心。
我希望它类似于此屏幕截图:

【问题讨论】:

标签: android


【解决方案1】:

您可以为顶部栏使用自己的布局,然后只需将自定义元素放入其中。

如下:

android.support.v7.app.ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayUseLogoEnabled(false);
mActionBar.setDisplayHomeAsUpEnabled(false);
mActionBar.setDisplayShowCustomEnabled(true);
mActionBar.setDisplayShowHomeEnabled(false);


ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.topbarclassic,null);
mActionBar.setCustomView(actionBarLayout);

我正在使用我制作的自定义布局,R.layout.topbarclassic 作为顶栏。

只需使用带有一些 gravity 的 LinearLayout 来正确放置元素,就完成了! :)

【讨论】:

  • 非常感谢,但我想在点击它们时。其中一个打开菜单(子项)
  • 您仍然可以,只需设置您的一个按钮的侦听器 :)
【解决方案2】:

试试这个

res/menu/main.xml

<?xml version="1.0" encoding="utf-8"?>

<item android:id="@+id/app_icon"
    android:icon="@drawable/your_app_icon"
    android:title="@string/app_icon"
    app:showAsAction="always"/>

在你的java代码

中覆盖onCreateOptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu){

    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多