如何将操作栏选项卡对齐到右侧?

3
我已经以编程方式添加了操作栏选项卡。我不知道如何将操作栏选项卡对齐到右侧。
ActionBar bar = getActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // instantiate tabs
        ActionBar.Tab pageTab= bar.newTab().setText(getString(R.string.page))
                .setIcon(R.drawable.ic_page);
bar.addTab(pageTab);

        // other settings
        bar.setDisplayOptions(ActionBar.DISPLAY_USE_LOGO | ActionBar.DISPLAY_SHOW_TITLE);
        bar.setDisplayShowHomeEnabled(true);

        // remove the activity title to make space for tabs
        bar.setDisplayShowTitleEnabled(false);

分享你的代码。使用布局参数,你可以将它添加到右侧或通过你的布局来使用。 actionBarView = getLayoutInflater().inflate(R.layout.custom_action_bar_view, null); - Padma Kumar
1
你好 :) 我该如何使用布局参数实现这个呢..? - qme
3个回答

3

操作栏选项卡位于左侧。右侧用于您的选项菜单和从中提升为工具栏按钮的任何项目。


2

原问题已经有一段时间了,但是...由于活动的视觉外观,我需要解决同样的问题。事实证明,如果您将空格附加到标题或副标题中的任意一个,选项卡将向右移动,因此您可以这样做:

bar.setTitle("Some titile\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");

这将移动整个选项卡栏以适应白色空间的空间。我必须说,这不是一个优雅的解决方案,但如果您在操作栏中没有任何操作,则可以使用。 (我没有检查存在操作时会发生什么。)


-1

我通过自定义应用程序主题成功将操作栏向右对齐

在我的AndroidManifest.xml文件中,我添加了一个android:theme属性:

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

我接着在我的项目中添加了一个新的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">right</item>
    </style>
</resources>

现在应该将选项卡右对齐。

编辑: 仅当您的操作栏在纵向方向上显示为2行时才有效。


@Populus 为什么要踩这个答案?这个解决方案是有效的,并且已经被接受作为另一个问题的答案。 - Goldorak84
顺便提一下,它只在操作栏在两行显示时才能正常工作,尤其是在纵向模式下。我更新了我的答案以反映这一点。 - Goldorak84

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接