0

スタイルの異なる 2 つのタブを作成したいと考えています。例のような結果を得るにはどうすればよいですか?

例:

ここに画像の説明を入力

そして、私が今持っているもの:

ここに画像の説明を入力

タブアクティビティ

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

1 に答える 1

0

これは、タブに割り当てる背景によって制御されます (通常はスタイルとテーマによって)。タブのスタイルを作成します。

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

関連するオーバーライドが background 属性だけになるように、ActionBarSherlock スタイルを拡張しました。テーマ定義で、これらをテーマに追加します。

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

あとは、状態ごとに描画可能なリソースを作成し、背景用に状態リストの描画可能なリソースを含めるだけです。

編集: 2 つの異なるタブ スタイルがある場合:

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

アプリのテーマを作成し、それを拡張する 2 つ目のテーマを作成します。

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

マニフェストandroid:theme="@style/MyAppTheme"で、要素に設定し<application>ます。2 番目のタブ スタイルが必要なアクティビティの場合android:theme="@style/MyAppTheme.Alternate"は、<activity>要素で使用します。

于 2013-03-27T16:00:33.807 に答える