【问题标题】:Different style of Tabs (ActionBarSherlock)不同风格的标签(ActionBarSherlock)
【发布时间】:2013-03-17 17:35:04
【问题描述】:

我想创建两个不同样式的标签。我必须怎么做才能得到像示例中这样的结果?

例子:

而我现在拥有的:

TabsActivity

public class TabsActivity extends SherlockFragmentActivity {

    private TabsAdapter mTabsAdapter;
    private ActionBar mActionBar;
    private TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.l_maintab);

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            BitmapDrawable bg = (BitmapDrawable)
            getResources().getDrawable(R.drawable.bg_striped);         
            getSupportActionBar().setBackgroundDrawable(bg);

            BitmapDrawable bgSplit = (BitmapDrawable) 
            getResources().getDrawable(R.drawable.bg_striped_split_img);
            bgSplit.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
            getSupportActionBar().setSplitBackgroundDrawable(bgSplit);
        }

        final ActionBar bar = getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayOptions(0, ActionBar.DISPLAY_USE_LOGO);
        bar.setDisplayShowTitleEnabled(true);
        bar.setDisplayShowHomeEnabled(true);

        mTabsAdapter = new TabsAdapter(this, (ViewPager) findViewById(R.id.pager));

        ActionBar.Tab events_tab = bar.newTab().setText("Events");
        ActionBar.Tab volo_tab = bar.newTab().setText("Volunteers");

        mTabsAdapter.addTab(events_tab, EventFragment.class, null);
        mTabsAdapter.addTab(volo_tab, UserFragment.class, null);

        bar.selectTab(bar.getTabAt(tabnum));
    }

l_maintab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       />

</LinearLayout>

【问题讨论】:

    标签: android android-layout tabs styles actionbarsherlock


    【解决方案1】:

    这由您分配给选项卡的背景控制(通常通过样式和主题)。为标签创建样式:

    <style name="MyTabStyle" parent="Widget.Sherlock.ActionBar.TabView">
        <item name="android:background">@drawable/my_tab_indicator</item>
    </style>
    

    我扩展了 ActionBarSherlock 样式,以便唯一相关的覆盖是背景属性。在您的主题定义中,将这些添加到您的主题中:

    <item name="actionBarTabStyle">@style/MyTabStyle</item>
    <item name="android:actionBarTabStyle">@style/MyTabStyle</item>
    

    那么您需要做的就是为每个状态创建一个可绘制资源,并为您的背景添加一个状态列表可绘制资源。

    编辑: 如果您有两种不同的标签样式:

    <style name="MyTabStyle" parent="Widget.Sherlock.ActionBar.TabView">
        <item name="android:background">@drawable/my_tab_indicator</item>
    </style>
    
    <style name="MyOtherTabStyle" parent="Widget.Sherlock.ActionBar.TabView">
        <item name="android:background">@drawable/my_other_tab_indicator</item>
    </style>
    

    制作您的应用主题,然后制作第二个扩展主题:

    <style name="MyAppTheme" parent="Theme.Sherlock...">
        <!-- other theme attributes -->
        <item name="actionBarTabStyle">@style/MyTabStyle</item>
        <item name="android:actionBarTabStyle">@style/MyTabStyle</item>
    </style>
    
    <style name="MyAppTheme.Alternate">
        <!-- Override just the tab style -->
        <item name="actionBarTabStyle">@style/MyOtherTabStyle</item>
        <item name="android:actionBarTabStyle">@style/MyOtherTabStyle</item>
    </style>
    

    在您的 Manifest 中,在 &lt;application&gt; 元素中设置 android:theme="@style/MyAppTheme"。对于需要第二个选项卡样式的活动,请在 &lt;activity&gt; 元素中使用 android:theme="@style/MyAppTheme.Alternate"

    【讨论】:

    • 但是通过这种方式,我将为所有选项卡创建一个样式,而不是为每个选项卡创建自己的样式,是吗?
    • 也许我误解了你的问题。您可以做的是创建第二个选项卡样式,然后创建一个替代主题来扩展您当前的主题,并且只覆盖actionBarTabStyle 属性。为清单中具有第二种样式的选项卡的活动使用第二个主题。
    • 如果我每个标签有一个活动,您的解决方案将有效。但是我有一个 TabsActivity 和每个选项卡的一个片段。片段未在 AndroidManifest.xml 中声明,这就是我无法使用您的解决方案的原因。
    • ActionBar 是每个 Activity。你有一个 Activity,然后你有一个 ActionBar 和一组选项卡(你会使用我的第一个解决方案)。可能我还是不明白你的情况。如果您通过选择另一个选项卡来更改片段,那么为什么要更改选项卡的样式?这似乎是一种令人困惑的用户体验。
    • “这似乎是一种令人困惑的用户体验” - 是的,但我需要这样做。现在,你明白我的问题了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多