以编程方式更改方向更改的android操作栏选项卡文本颜色?

时间:2014-07-22 01:39:40

标签: android android-actionbar

我想更改标签中的文字颜色。如何引用我想要在函数内部更改颜色属性的选项卡布局:

public void onConfigurationChanged(Configuration newConfig) 
{
   findViewById(R.id.tab_textview); // returns null
}

因为这会返回null。 tab_textview是选项卡的模板。在onCreate中,我只是在操作栏中放置了标签,一切正常。我只需要在改变方向时改变颜色,这样文字就是白色可见。找到许多类似的问题,但我不能让它工作。我是android编程的新手。

1 个答案:

答案 0 :(得分:1)

onCreate方法中,我们像这样初始化ActionBar:

ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    Tab tab = actionBar
            .newTab()
            .setCustomView(R.layout.tab_textview) // use our TextView
            .setTabListener(
                    new Chapter1TabListener<FragmentA>(this, "fragmentA",
                            FragmentA.class));
    TextView tabview = (TextView) tab.getCustomView();
    tabview.setText("First Tab");
    actionBar.addTab(tab);
    tab = actionBar
            .newTab()
            .setCustomView(R.layout.tab_textview)
            .setTabListener(
                    new Chapter1TabListener<FragmentB>(this, "fragmentB",
                            FragmentB.class));
    tabview = (TextView) tab.getCustomView();
    tabview.setText("Second Tab");
    actionBar.addTab(tab);

覆盖onConfigurationChanged,请尝试以下操作:

super.onConfigurationChanged(newConfig);
    if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        ActionBar actionBar = getActionBar();
        for(int i=0; i<actionBar.getTabCount(); i++ ) {
            Tab tab = actionBar.getTabAt(i);
            TextView tv = (TextView) tab.getCustomView();
            tv.setTextColor(getResources().getColor(android.R.color.holo_blue_dark));
        }
    }