简体   繁体   English

如何在Android中使操作栏标签中心对齐?

[英]How can I make action bar tabs center align in Android?

Android action bar tabs are left-aligned by default, I want to align it the center of the action bar. Android操作栏选项卡默认情况下是左对齐的,我想将其对齐到操作栏的中心。 Is there any way we can make it center? 有什么办法可以使它居中吗?

I was able to center-align the actionbar by customizing my application theme 通过自定义应用程序主题,我能够使操作栏居中对齐

In my AndroidManifest.xml, I added a android:theme property: 在我的AndroidManifest.xml中,添加了android:theme属性:

<application
        android:label="@string/app_name"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/CustomActionBarTheme">
  ...

I then added a new themes.xml file to my project, and added : 然后,我向项目中添加了一个新的themes.xml文件,并添加了:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarTabBarStyle">@style/MyActionBarTabBar</item>
    </style>

    <style name="MyActionBarTabBar">
        <item name="android:gravity">center</item>
    </style>
</resources>

The tabs should now be center-aligned. 选项卡现在应该居中对齐。

EDIT: It works only if your actionbar is displayed on 2 lines (in portrait) 编辑:仅当您的操作栏显示为两行(纵向)时,此功能才有效

You can use the ViewPagerIndicator plugin to customize your tabs. 您可以使用ViewPagerIndicator插件来自定义标签。

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM