android更改FragmentTabHost背景和文本颜色

时间:2014-12-22 11:38:28

标签: android android-tabhost android-styles fragment-tab-host

我正在工作FragmentTabHost。我创建了FragmentTabHost,我可以替换一些片段。现在我想更改FragmentTabHost中的背景颜色以及文字颜色。例如我想要背景颜色黑色和文本颜色white.this是我的xml代码

<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
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" >

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ff0000"/>

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

这是我的java代码

 public class CustomTabActivity extends FragmentActivity {
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

        mTabHost.addTab(
                mTabHost.newTabSpec("tab1").setIndicator("Tab 1", null),
                StradaMenu.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("tab2").setIndicator("Tab 2", null),
                StradaMenu.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("tab3").setIndicator("Tab 3", null),
                StradaMenu.class, null);
    }

}

我如何解决我的问题?如果有人知道解决方案,请帮助我 感谢

1 个答案:

答案 0 :(得分:0)

您必须使用操作栏选项卡为每个选项卡获取不同的颜色。

查看this

<强> MainActivity.java:

public class MainActivity extends FragmentActivity {
 static ViewPager Tab;
 TabsPagerAdapter TabAdapter;
 ActionBar actionBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TabAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    Tab = (ViewPager) findViewById(R.id.pager);

    Tab.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar = getActionBar();
            actionBar.setSelectedNavigationItem(position);
        }
    });

    Tab.setAdapter(TabAdapter);
    actionBar = getActionBar();
    // Enable Tabs on Action Bar
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.TabListener tabListener = new ActionBar.TabListener() {
        @Override
        public void onTabReselected(android.app.ActionBar.Tab tab,
                FragmentTransaction ft) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            Tab.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(android.app.ActionBar.Tab tab,
                FragmentTransaction ft) {
            // TODO Auto-generated method stub
        }
    };

    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

    ActionBar.Tab tab = actionBar.newTab().setText("Home")
            .setTabListener(new TabListener(this, Home.class.getName()));
    View tabView = inflater.inflate(R.layout.fragment_home, null);
    tabView.setBackgroundResource(R.drawable.gradient_shape); // set custom
                                                                // color

    tab.setCustomView(tabView);
    actionBar.addTab(tab);

    tab = actionBar.newTab().setText("News")
            .setTabListener(new TabListener(this, News.class.getName()));
    View tabView2 = inflater.inflate(R.layout.fragment_news, null);
    tabView2.setBackgroundResource(R.drawable.gradient_shape2); // set
                                                                // custom
                                                                // color
    tab.setCustomView(tabView2);
    actionBar.addTab(tab);

    tab = actionBar.newTab().setText("Latest")
            .setTabListener(new TabListener(this, Latest.class.getName()));
    View tabView3 = inflater.inflate(R.layout.fragment_latest, null);
    tabView3.setBackgroundResource(R.drawable.gradient_shape3); // set
                                                                // custom
                                                                // color
    tab.setCustomView(tabView3);
    actionBar.addTab(tab);

  }

public static class TabListener extends Fragment implements
        ActionBar.TabListener {

    public TabListener(MainActivity mainActivity, String name) {
        // this(mainActivity,name);
    }

    @Override
    public void onTabSelected(android.app.ActionBar.Tab tab,
            FragmentTransaction ft) {
        Tab.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(android.app.ActionBar.Tab tab,
            FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(android.app.ActionBar.Tab tab,
            FragmentTransaction ft) {

    }

}

}