android - 如何更改 android 选项卡主机中选定选项卡的颜色?

标签 android tabs android-tabhost tabwidget

我已经有了一个选项卡小部件,它可以在被选中时更改其子选项卡的颜色。但这是有问题的,我的 android 选项卡小部件的边框线并不是一直运行良好。有时,当我选择另一个选项卡以使其清晰时,边框线会变成白色,这是我的 Activity 代码。如果我不能提供图片,我很抱歉,因为我在 stack overflow 上没有赢得很多声誉

@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>

最佳答案

首先,在我的 Activity 中,我实例化了我的选项卡主机

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

希望对你有帮助

关于android - 如何更改 android 选项卡主机中选定选项卡的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26878303/

相关文章:

javascript - 光滑的 slider 在 jQuery 选项卡中不起作用

android - 在嵌套的 FragmentTabHost 中切换选项卡后 fragment 不可见

java - 为 android 自定义选项卡标题中的文本

java - split() 无法正常工作

android - 拖放列表

java - AdjustPan 在某些布局中不起作用

javascript - 单击选项卡后,Angular Bootstrap 选项卡更改 url

jQuery ui 选项卡锚定,如何防止?

android - 将控件动态添加到 tabhost 时出现问题

java - 某些 mipmap 不显示