如何在android标签主机中更改所选标签的颜色?

时间:2014-11-12 02:35:54

标签: android tabs android-tabhost tabwidget

我已经有一个标签窗口小部件,可以在选中时更改其子标签颜色。但这是有缺陷的,我的android选项卡小部件的边界线不能一直运行良好。当我选择另一个标签使其清晰时,有时边框线会变为白色,这是我的活动代码。对不起,如果我无法提供图片,因为我在堆栈溢出时没有获得很多声誉

@SuppressWarnings("deprecation")
public class TabHostActivity extends TabActivity {

    private TabHost tabHost;
    private int currentTab = 0;
    private int lastTab = 0;

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

            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

            setContentView(R.layout.activity_tabhost);
            UserAccount userAccount = new UserAccount();

            tabHost = (TabHost) findViewById(android.R.id.tabhost);


            TabSpec tab1 = tabHost.newTabSpec("First Tab");
            tab1.setContent(new Intent(this, HomeActivity.class));
            tab1.setIndicator("",getResources().getDrawable(R.drawable.home_icon));
            tabHost.addTab(tab1);

            TabSpec tab2 = tabHost.newTabSpec("Second Tab");
            tab2.setContent(new Intent(this, About.class));
            tab2.setIndicator("",getResources().getDrawable(R.drawable.about_icon));
            tabHost.addTab(tab2);


            TabSpec tab3 = tabHost.newTabSpec("Third Tab");
            tab3.setContent(new Intent(this, GridViewActivity.class));
            tab3.setIndicator("",getResources().getDrawable(R.drawable.gallery_icon));
            tabHost.addTab(tab3);


            getTabHost().setOnTabChangedListener(new OnTabChangeListener() {
                 public void onTabChanged(String tabId)
                 {
                        currentTab = getTabHost().getCurrentTab();

                        setCurrentTabColor();

                        lastTab =currentTab;
                 }
            });

            getTabWidget().getChildAt(lastTab).setBackgroundColor(Color.GRAY);
        }

        public void setCurrentTabColor(){
            getTabWidget().getChildAt(currentTab).setBackgroundColor(Color.GRAY);
            getTabWidget().getChildAt(lastTab).setBackgroundColor(Color.parseColor("#FCAFA6"));
        }
}

这是我的tabhost的xml,它将我的标签小部件的背景颜色设置为粉红色

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

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

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:background="#FCAFA6" >
        </TabWidget>
    </LinearLayout>

</TabHost>

1 个答案:

答案 0 :(得分:0)

首先,在我的活动中,我实例化了我的标签主机

tabHost = (FragmentTabHost) findViewById(R.id.tabContainer);
tabHost.setup(this, getSupportFragmentManager(), R.id.tabContent);
    tabHost.addTab(tabHost.newTabSpec("1").setIndicator("New Tab"),
            PageFragment.class, null);

我写了一个方法来根据是否选中了标签的颜色来更新标签的颜色

protected void updateTabs() {



    for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {

        if (tabHost.getTabWidget().getChildAt(i).isSelected()) {
            SessionState.getSelectedIndex().setSelectedPage(i);
            tabHost.getTabWidget()
                    .getChildAt(i)
                    .setBackgroundResource(
                            R.drawable.tab_selected_background);
        } else {

            tabHost.getTabWidget()
                    .getChildAt(i)
                    .setBackgroundResource(
                            R.drawable.tab_unselected_background);

        }
    }

}

然后每次选项卡更改时都调用该方法

tabhost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
            updateTabs();
        }
    });

我希望这会有所帮助