android - 使用自定义 View 复制 ActionBar 选项卡

标签 android android-actionbar actionbarsherlock

我想要一个带有自定义导航的 ActionBar,其中自定义 View 看起来像标准操作栏选项卡。我知道这听起来像是在重新发明轮子,但这意味着我们可以将菜单按钮与选项卡放在同一行,如下所示。这是一项设计要求,与标准的 Android 行为相比,此应用程序的 UI 实际上更具意义。 How I would like the tabs to look

我试过像这样使用 ActionBarSherlock 中的 IcsLinearLayout:

<IcsLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:orientation="horizontal"
          android:layout_height="50dip">
         <Button
             android:id="@+id/tab_1"
             android:layout_height="match_parent"
             android:gravity="center"
             android:layout_width="wrap_content"
             android:textStyle="bold"
             android:text="TAB_1"
             android:background="@drawable/abs__item_background_holo_light"
             />
        <Button
            android:id="@+id/tab_2"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:textStyle="bold"
            android:text="TAB_2"
            android:background="@drawable/abs__item_background_holo_light"
             />
</IcsLinearLayout>

但这会复制 ActionButtons,我不知道如何复制 Tabs。

我想我需要:

  • 一个特殊的选项卡容器 View 组(可能来自 ActionBarSherlock 库)
  • 看起来像标签的 View 来自 ABS 库的背景资源。
  • 一些代码表明 单击 View 后,它仍保持选中状态(类似于 单选按钮)。

任何指向示例或类似解决方案(甚至在 ActionBarSherlock 库中)的指针都将不胜感激。

最佳答案

//启用嵌入式标签

//pre-ICS
if (actionBarSherlock instanceof ActionBarImpl) {
    enableEmbeddedTabs(actionBarSherlock);

//ICS and forward
} else if (actionBarSherlock instanceof ActionBarWrapper) {
    try {
        Field actionBarField = actionBarSherlock.getClass().getDeclaredField("mActionBar");
        actionBarField.setAccessible(true);
        enableEmbeddedTabs(actionBarField.get(actionBarSherlock));
    } catch (Exception e) {
        Log.e(TAG, "Error enabling embedded tabs", e);
    }
}

//helper method
private void enableEmbeddedTabs(Object actionBar) {
    try {
        Method setHasEmbeddedTabsMethod = actionBar.getClass().getDeclaredMethod("setHasEmbeddedTabs", boolean.class);
        setHasEmbeddedTabsMethod.setAccessible(true);
        setHasEmbeddedTabsMethod.invoke(actionBar, true);
    } catch (Exception e) {
        Log.e(TAG, "Error marking actionbar embedded", e);
    }
}

此代码完美运行。在我的应用程序中尝试过。 供进一步引用 - https://groups.google.com/forum/#!topic/actionbarsherlock/hmmB1JqDeCk

关于android - 使用自定义 View 复制 ActionBar 选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12392541/

相关文章:

android - Okhttp4,无法访问 'body' : it is package-private in 'Response'

android - 如何从android中的getDropDownView方法获取微调器ID或标签

android - AnimatedVectorDrawable 圆到线

android - 打算在 fragment 类中

android - 更改选项卡标题的字体 - ActionBarSherlock

android - 防止 Android ActionBar 滚动

android - 如何在 Android 端动画加载 GIF?

android - ActionBar 列表导航重叠 fragment

android - preferenceactivity 中没有带有 app-compat-v21 库的 actionBar?

android - 如何设置操作栏菜单组的可见性?