Change the tabhost style android

落爺英雄遲暮 提交于 2020-01-06 08:14:19

问题


I want to change the default blue color of the tabhost to red.

<style name="AppTheme" parent="android:Theme.Light.NoTitleBar">
          <item name="android:tabWidgetStyle">@drawable/tab_indicator_holo</item>
          </style>

tab_indicator_holo.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_holo" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected_holo" />

    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_focused_holo" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected_focused_holo" />

    <!-- Pressed -->
    <!--    Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_holo" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_holo" />

    <!--    Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_holo" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_holo" />
</selector>

But the tab style is not applied to the tabhost. The default blue color is not changed to red.

I am getting this

Any ideas or suggestions please.


回答1:


You might have already found the answer, but for those who may face the same problem, Here is what I have done.

  1. go to custom holo theme and set tabwedget to yes and choose preferred color.

  2. Download the zip, copy over to my project.

  3. Add to tabadapter with the view created by inflating tab_indicator_holo.

    View mIndicator = inflater.inflate(R.layout.tab_indicator_holo, mTabHost.getTabWidget(), false);
    TextView title1 = (TextView) mIndicator.findViewById(android.R.id.title);

    title1.setText("TAB1");

    mTabsAdapter.addTab(mTabHost.newTabSpec("TAB1").setIndicator( mIndicator), FRAGMENT1.class, null);

    View mIndicator2 = inflater.inflate(R.layout.tab_indicator_holo,      mTabHost.getTabWidget(), false);
    TextView title2 = (TextView) mIndicator2.findViewById(android.R.id.title);

    title2.setText("TAB2");
    mTabsAdapter.addTab(mTabHost.newTabSpec("TAB2").setIndicator(mIndicator2), FRAGMENT2.class, null);


来源:https://stackoverflow.com/questions/17649217/change-the-tabhost-style-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!