使用自定义视图复制ActionBar选项卡

5
我希望能有一个自定义导航的 ActionBar,其中自定义视图看起来像标准 ActionBar 标签。我知道这听起来像是重新发明轮子,但这意味着我们可以像下面展示的那样把菜单按钮放在同一行标签上。这是一个设计要求,在这个应用程序中,它比标准的 Android 行为更加实用且更合理地利用了 UI。 我希望选项卡的外观如何

我尝试使用 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。

我认为我需要:

  • 一个特殊的选项卡容器ViewGroup(可能来自ActionBarSherlock库)
  • 看起来像选项卡的视图,具有ABS库中的背景资源。
  • 一些代码表明在视图被点击后它仍然被选中(类似于RadioButton)。

任何关于示例或类似解决方案的提示(即使在ActionBarSherlock库中),都将非常感激。

2个回答

14

//启用嵌入式标签页

//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


做得很好!只是需要注意的是,在设置其余的操作栏选项卡等之前,需要先完成这个步骤。 - DavidBriggs

6

通过使用层次结构查看器,我认为我们已经找到了如何实现这一点。事实证明,这并不困难。您需要使用ABS库中的ScrollingTabContainerView,并可以向其中添加选项卡。

带有选项卡的窄操作栏

public class MainActivity extends SherlockActivity implements ActionBar.TabListener {
/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ScrollingTabContainerView root = new ScrollingTabContainerView(this);
    ActionBar.Tab tab1 = getSupportActionBar().newTab();
    tab1.setText("TAB 1");

    tab1.setTabListener(this);

    ActionBar.Tab tab2 = getSupportActionBar().newTab();
    tab2.setText("TAB 2");
    tab2.setTabListener(this);

    root.addTab(tab1, 0, true);
    root.addTab(tab2, 1, false);

    getSupportActionBar().setCustomView(root);
    getSupportActionBar().setDisplayShowCustomEnabled(true);
    getSupportActionBar().setTitle("App Title");

}

@Override
public boolean onCreateOptionsMenu(Menu menu){
    menu.add("MENU ITEM 1");
    menu.add("MENU ITEM 2");
    return true;
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
    //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    //To change body of implemented methods use File | Settings | File Templates.
}

希望这能对某些人有所帮助。


这在ICS和Jellybean中不起作用:(有什么解决方法吗? - nia
嗯,自上周以来我还没有时间研究这个问题,但我会尽力解决。 - DavidBriggs
在研究了一段时间后,我决定这确实是一个相当可怕的解决方案,它在ICS以上不起作用,而且真的不应该得到鼓励。也许有人某天可以解决它... - DavidBriggs
这段代码可以工作,但是onTabListener没有被调用。只有当我们添加了getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);之后,选项卡才会出现在第二行,位于操作栏下方。如何正确处理? - JULI
哈哈 =) 如果结合这篇和之前的文章,它就能正常工作。太好了! - JULI
显示剩余2条评论

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