【问题标题】:How to style ActionBar's Tabar looks like tabhost如何设置 ActionBar 的 Tabbar 看起来像 tabhost 的样式
【发布时间】:2014-11-10 14:41:31
【问题描述】:

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

我之前的情况是这样的:

我现在是这样的:

我只是不知道如何自定义操作栏的选项卡以显示与以前相同。我不需要滚动标签,只需要固定的老式标签。

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

我尝试过使用风格技巧进行挖掘,但到目前为止还没有运气。我可以改变颜色,但不能改变宽度。看来我确实需要使用 AppComp 主题或其下层来让应用程序在旧设备上运行。

任何帮助将不胜感激...谢谢!

【问题讨论】:

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


【解决方案1】:

我已将其添加到屏幕底部,您可以根据需要更改它

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>

我是这样弄的,有什么问题可以问

【讨论】:

  • 谢谢!我看到您使用的是 Tabhost 而不是 Actionbar 的选项卡。如果我找不到让操作栏版本正常工作的方法,我会尝试你的解决方案。非常感谢!
  • 没问题,如果您使用我的代码并遇到任何问题,请告诉我。尝试帮助解决它。
猜你喜欢
  • 2014-07-22
  • 1970-01-01
  • 2023-03-03
  • 2012-03-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-01
  • 2016-07-22
  • 2018-08-24
相关资源
最近更新 更多