簡體   English   中英

以編程方式更改方向上的android actionbar選項卡文本顏色嗎?

[英]Change android actionbar tab text color on orientation change programaticly?

我想更改標簽中文本的顏色。 如何引用要在函數內部更改color屬性的選項卡布局:

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

由於此返回null。 tab_textview是選項卡的模板。 在onCreate中,我只是將選項卡放在操作欄中,一切正常。 我只需要在更改方向時更改顏色,即可使文本為白色且可見。 發現許多類似的問題,但我無法使其正常工作。 我對android編程非常陌生。

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));
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM