如何设置样式ActionBar的Tabar看起来像tabhost

时间:2014-11-10 06:13:20

标签: android android-actionbar android-support-library android-tabs

之前我在tabhost中有5个标签,使用旧的2.2 SDK和Activity组。现在我需要将它们更改为ActionBar及其tabbar,具有相似的外观。

我之前拥有的是这样的: Before

我现在拥有的是这样的:

now

我无法弄清楚如何自定义操作栏的标签以显示与之前相同的内容。我不需要滚动标签,只需要固定,旧时尚。

我使用Support Libiray,项目中的v4和v7都支持最高sdk 8,这是必需的。

我尝试过挖掘风格,但到目前为止还没有运气。我可以改变颜色,但不能改变宽度。而且我似乎需要使用AppComp主题或其desenct来使应用程序在旧设备上运行。

任何帮助都将非常感谢...谢谢!

1 个答案:

答案 0 :(得分:0)

我已将其添加到屏幕底部,您可以将其更改为您的要求

MainActivity.java

public class MainActivity extends Activity {
public static final String M_CURRENT_TAB = "M_CURRENT_TAB";
private TabHost mTabHost;
private String mCurrentTab;
TabHost.TabSpec spec;
public static final String TAB_PROFILE = "TAB_PROFILE";
public static final String TAB_GAMES = "TAB_GAMES";
public static final String TAB_VIDEO = "TAB_VIDEO";
public static final String TAB_MUSIC = "TAB_MUSIC";
public static final String TAB_MIAPPS = "TAB_MIAPPS";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    getActionBar().hide();
    setContentView(R.layout.activity_main);

    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();

    if (savedInstanceState != null) {
        mCurrentTab = savedInstanceState.getString(M_CURRENT_TAB);
        initializeTabs();
        mTabHost.setCurrentTabByTag(mCurrentTab);

        mTabHost.setOnTabChangedListener(listener);
    } else {
        mTabHost.setOnTabChangedListener(listener);
        initializeTabs();
    }

}

private View createTabView(final int id, final String text) {
    View view = LayoutInflater.from(this).inflate(R.layout.tabs_icon, null);
    ImageView imageView = (ImageView) view.findViewById(R.id.tab_icon);
    imageView.setImageDrawable(getResources().getDrawable(id));
    TextView textView = (TextView) view.findViewById(R.id.tab_text);
    textView.setText(text);
    return view;
}

public void initializeTabs() {

    // GAMES
    spec = mTabHost.newTabSpec(TAB_GAMES);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(R.id.realtabcontent);
        }
    });
    spec.setIndicator(createTabView(R.drawable.tabselector_game, ""));
    mTabHost.addTab(spec);

    // VIDEO
    spec = mTabHost.newTabSpec(TAB_VIDEO);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(R.id.realtabcontent);
        }
    });
    spec.setIndicator(createTabView(R.drawable.tabselector_video, ""));
    mTabHost.addTab(spec);

    // MUSIC
    spec = mTabHost.newTabSpec(TAB_MUSIC);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(R.id.realtabcontent);
        }
    });
    spec.setIndicator(createTabView(R.drawable.tabselector_music, ""));
    mTabHost.addTab(spec);

    // MI-APPS
    spec = mTabHost.newTabSpec(TAB_MIAPPS);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(R.id.realtabcontent);
        }
    });
    spec.setIndicator(createTabView(R.drawable.tabselector_miapps, ""));
    mTabHost.addTab(spec);

    // USER PROFILE
    spec = mTabHost.newTabSpec(TAB_PROFILE);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(R.id.realtabcontent);
        }
    });
    spec.setIndicator(createTabView(R.drawable.tabselector_user, ""));
    mTabHost.addTab(spec);

}

TabHost.OnTabChangeListener listener = new TabHost.OnTabChangeListener() {
    @SuppressLint("NewApi")
    public void onTabChanged(String tabId) {

        mCurrentTab = tabId;

        if (tabId.equals(TAB_GAMES)) {
            pushFragments(new GamesFragment(MainActivity.this), false,
                    false, null);
        } else if (tabId.equals(TAB_VIDEO)) {
            pushFragments(new VideoFragment(), false, false, null);
        } else if (tabId.equals(TAB_MUSIC)) {
            pushFragments(new MusicFragment(), false, false, null);
        } else if (tabId.equals(TAB_MIAPPS)) {
            pushFragments(new MiAppsFragment(), false, false, null);
        } else if (tabId.equals(TAB_PROFILE)) {
            pushFragments(new UserProfileFragment(), false, false, null);
        }

    }
};

public void pushFragments(Fragment fragment, boolean shouldAnimate,
        boolean shouldAdd, String tag) {
    FragmentManager manager = getFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();
    // if (shouldAnimate) {
    // ft.setCustomAnimations(R.animator.fragment_slide_left_enter,
    // R.animator.fragment_slide_left_exit,
    // R.animator.fragment_slide_right_enter,
    // R.animator.fragment_slide_right_exit);
    // }
    ft.replace(R.id.realtabcontent, fragment, tag);

    if (shouldAdd) {
        /*
         * here you can create named backstack for realize another logic.
         * ft.addToBackStack("name of your backstack");
         */
        ft.addToBackStack(null);
    } else {
        /*
         * and remove named backstack:
         * manager.popBackStack("name of your backstack",
         * FragmentManager.POP_BACK_STACK_INCLUSIVE); or remove whole:
         * manager.popBackStack(null,
         * FragmentManager.POP_BACK_STACK_INCLUSIVE);
         */
        manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }
    ft.commit();
}

}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />

        <FrameLayout
            android:id="@+android:id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="60dip"
            android:showDividers="none"
            android:layout_weight="0"
            android:orientation="horizontal" />
    </LinearLayout>
</TabHost>

<LinearLayout
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#ffffff"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight=".3" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@drawable/ic_launcher" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="#ccdedede"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="90" />
        </LinearLayout>
    </FrameLayout>

    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight=".7"
        android:choiceMode="singleChoice"
        android:dividerHeight="1dp" />
</LinearLayout>

我这样做了,如果有任何疑问,请随时询问