【问题标题】:Android ActionBarSherlock Top BarAndroid ActionBarSherlock 顶栏
【发布时间】:2012-07-10 11:58:54
【问题描述】:

我想要一个像foursquare这样的Action Bar。我想要的是标签,例如 Friends、Explore 和 Me。此外,在选项卡上方,我希望有一个自定义布局,其中包括一些按钮,例如 Foursquare 中的 foursquare 徽标、刷新和签入。我创建了选项卡,但无法更改 ActionBarSherlock 中选项卡上方的布局。我该如何解决这个问题?

【问题讨论】:

  • 您能给我们提供一个 Foursquare 应用程序的打印屏幕吗?
  • 我加了foursquare的ps。
  • 我很确定他们使用的是个人实现的栏,他们没有使用 ActionBarSherlock。
  • @OvidiuLatcu 您一定是对的,但您知道如何使用 ActionBarSherlock 在选项卡上方添加自定义布局吗?

标签: android tabs actionbarsherlock


【解决方案1】:

要实现 Foursquare 外观,您无需考虑在选项卡上方创建布局。使用 Actionbar Sherlock 时您只需要担心:

  1. 为顶部栏制作背景
  2. 为顶部栏制作徽标
  3. 在 menu.xml 文件中添加项,ActionbarSherlock 将使用这些项用按钮填充顶部(只要将使用 Actionbar Sherlock 样式的样式附加到 thw 活动)。

所以,对于 1. 和 2. 来说,就是使用 styles.xml 文件(应该位于 res 文件夹中的 values 文件夹中),如下所示:

<style name="Theme.Example" parent="Theme.Sherlock">
    <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
    <item name="absForceOverflow">true</item>       
</style>

<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.ActionBar.Solid">
    <item name="background">@drawable/actionbar_background</item> <-- the background for top bar
    <item name="icon">@drawable/actionbar_logo</item> <-- the logo that goes top left in the top bar
    <item name="backgroundSplit">@drawable/bg_striped_split</item> <-- the image used between the menu items
</style>

对于 3. 你需要做的就是在 menu.xml 下创建菜单项(应该驻留在 menu 文件夹中(如果没有,在 res 文件夹中创建一个)):

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">     

<item android:id="@+id/menu_prefs"
      android:icon="@drawable/settings_icon"
      android:title="Preferences"
      android:showAsAction="ifRoom" /> 
</menu>

查看菜单项您要做的最后一件事是在活动中使用这些功能:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

public boolean onOptionsItemSelected (MenuItem item) {

    Intent intent;

    switch (item.getItemId())
    {   
    case R.id.menu_prefs:

        // Launch Preference Activity
        intent = new Intent(getBaseContext(), Preferences.class);           
        startActivity(intent);
        break;
    }

    return false;
}

【讨论】:

  • 还有另一个问题,由于您的代码,我希望在单击 menu_prefs 时拥有 ListNavigation 或 ActionMode。此外,还有一个选项卡。我该如何管理?
  • @Richard Lewin - 使用上面的示例时,“蓝色”顶部导航出现在我的选项卡下方——知道为什么,以及如何将它放在上面吗?此外,当景观顶部栏折叠到选项卡中 - 无论如何要强制它留在顶部?
  • 顶部栏折叠是默认操作栏行为,因此不应(据我所知不能)更改。出现在选项卡下方的栏的问题是不寻常的,因为选项卡应该位于导航栏下方并且应该折叠到导航栏中。正在发生的事情的屏幕截图将有很大帮助。你能通过 Stack Overflow 发给我吗?
  • @RichardLewin 没问题,这里:stackoverflow.com/questions/12356422/… 谢谢。
【解决方案2】:

要设置您的自定义操作项(刷新、签入、...),您必须覆盖 onCreateOptionsMenu(Menu menu) 并设置您的自定义菜单。

例如:

文件菜单/my_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/refresh"
        android:icon="@drawable/ic_menu_refresh"
        android:title="refresh"
        android:showAsAction="always">
    </item>

</menu>

然后在您的活动(或片段)中:

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
  MenuInflater inflater = getSupportMenuInflater();
  inflater.inflate(R.menu.my_menu, menu);

  // Allow activity and fragments to add items
  super.onCreateOptionsMenu(menu);

  return true;
}

并且要在它们被选中时得到通知,只需覆盖 onOptionsItemSelected(MenuItem item):

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
  switch (item.getItemId())
  {
    case android.R.id.refresh :
      // refresh your data...
      return true;

    default :
      return super.onOptionsItemSelected(item);
  }
}

【讨论】:

  • @Chinaski - 与上面相同:使用上面的示例时,“蓝色”顶部导航出现在我的标签下方 - 知道为什么,以及如何将它放在上面吗?此外,当景观顶部栏折叠到选项卡中 - 无论如何要强制它保持在顶部?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-21
  • 1970-01-01
  • 2014-05-03
  • 1970-01-01
  • 1970-01-01
  • 2013-08-16
相关资源
最近更新 更多