代码之家  ›  专栏  ›  技术社区  ›  user3345767

Android tabhost更改文本颜色样式

  •  16
  • user3345767  · 技术社区  · 11 年前

    尝试更改tabhost文本颜色,在此代码中,我可以更改tabhost背景颜色(而不是文本颜色)

    tabHost.setOnTabChangedListener(new OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
              for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
                tabHost.getTabWidget().getChildAt(i)
                                .setBackgroundColor(Color.parseColor("#FF0000")); // unselected
              }
    
              tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
                            .setBackgroundColor(Color.parseColor("#0000FF")); // selected
    
            }
    });
    

    如何更改tabhost文本颜色?

    4 回复  |  直到 11 年前
        1
  •  54
  •   Mukesh Kumar Singh    11 年前

    您可以按如下方式更改Tabhost文本的颜色。

    tabHost.setOnTabChangedListener(new OnTabChangeListener() {
    
        @Override
        public void onTabChanged(String tabId) {
    
            for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
                tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); // unselected
                TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
                tv.setTextColor(Color.parseColor("#ffffff"));
            }
    
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected
            TextView tv = (TextView) tabhost.getCurrentTabView().findViewById(android.R.id.title); //for Selected Tab
            tv.setTextColor(Color.parseColor("#000000"))
    
        }
    });
    

    编辑:

    要在活动中最初设置文本颜色,可以在 onResume() 作用

    TabHost tabhost = getTabHost();
        for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) 
        {
            TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
            tv.setTextColor(Color.parseColor("#000000"));
        } 
    
        2
  •  12
  •   Will Molter    8 年前

    这实际上可以使用XML主题来完成。这个 TabWidget 使用 android:textColorPrimary 对于所选选项卡和 android:textColorSecondary 对于未选中的。因此,您可以实现如下文本颜色更改:

    在styles.xml中:

    <style name="TabWidgetTheme" parent="AppTheme">
        <item name="android:textColorPrimary">@color/your_primary_color</item>
        <item name="android:textColorSecondary">@color/your_secondary_color</item>
    </style>
    

    在布局中:

    <TabHost
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:theme="@style/TabWidgetTheme"/>
    

    注意 android:theme 不应直接在 选项卡小部件 而是包含 TabHost 或类似。

        3
  •  4
  •   Ankit Dhadse    11 年前

    要更改选项卡的文本颜色,您需要获取视图,即设置为选项卡标题的TextView,您可以这样更改:

    TabHost tabhost = getTabHost();
        for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) 
        {
            TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
            tv.setTextColor(Color.parseColor("#000000"));
        } 
    

    编辑:

    另一种方法是为选项卡创建自定义视图。将选项卡添加到tabHost时

    FragmentTabHost tabHost;
    
    tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
            tabHost.setup(this, getSupportFragmentManager(), R.id.frame);
    

    //为每个选项卡创建customView 视图tabViewHome=创建TabView(tabHost.getContext(),“Home”,R.drawbable.ic_Home);

    tabHost.addTab(tabHost.newTabSpec("Home").setIndicator(tabViewHome), HomeActivity.class, null);
    
    
    private static View createTabView(final Context context, final String text, int iconId)
        {
                // inflate your layout here
            View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
            TextView tv = (TextView) view.findViewById(R.id.tab_tv_title);
            tv.setText(text);
                tv.setTextColor(Color.RED);
            ImageView iv = (ImageView) view.findViewById(R.id.tab_background_iv_icon);
            iv.setImageResource(iconId);
            return view;
        }
    

    tab_layout.xml将如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tabsLayout"
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="5dip" 
        android:background="#AAE1E1E1">
    
         <ImageView
            android:id="@+id/tab_background_iv_icon"
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:contentDescription="@string/imgDesc"
            />
    
        <TextView
            android:id="@+id/tab_tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            //android:textColor="@drawable/tab_text_selector"
            android:textSize="8sp"
            android:textStyle="bold" />
    
    </LinearLayout>
    

    希望这有帮助。

        4
  •  0
  •   user3024665    10 年前

    我用这个解决方案:

    private void setNewTab(final String tag, final String title, final Class<?> clazz, final Bundle bundle) {
        TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag);
        tabSpec.setIndicator(InfoTabView_.build(getActivity()).bind(title, false));
        tabHost.addTab(tabSpec, clazz, bundle);
    }
    
    ...
    
    bundle = new Bundle();
    bundle.putSerializable(FaqFragment.ARG_FAQS, infos.getFaq());
    setNewTab("faq", "Faq", FaqFragment_.class, bundle);
    
    ...
    
    
    @EViewGroup(R.layout.view_info_tab)
    public class InfoTabView extends RelativeLayout {
    
        ....
    
        @Override
        public void setSelected(final boolean selected) {
            if (selected)
                titleTextView.setTextColor(selectedColor);
            else
                titleTextView.setTextColor(unselectedColor);
        }
    }
    

    覆盖setSelected是最干净的方法!