Android how to highlight drawable of selected action bar tab

依然范特西╮ 提交于 2019-12-11 03:22:40

问题


I am looking to add action bar tabs with icons. I have achieved the following.

In this, how do I make the selected tab icon orange i.e. if the first tab is selected, the first icon is orange, while the others stay gray and so on. Thanks


回答1:


Possible solution is. You must create an icon in white and orange and put it in your drawables folder and in the onCreate method of your currently tab add this here:

//TabActivity.onCreate()
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;

intent = new Intent().setClass(this,YourClass.class);
spec = tabHost.newTabSpec("tab_name").setIndicator("Tab Text", getResources().getDrawable(R.drawable.ic_tab_dialer)).setContent(intent);
tabHost.addTab(spec);

Then, you need to add ic_tab_dialer.xml to res/drawable/ directory with this content:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
        android:state_pressed="false"
        android:drawable="@drawable/ic_tab_selected_dialer" />
    <item android:drawable="@drawable/ic_tab_unselected_dialer" />
</selector>

I hope this helps.



来源:https://stackoverflow.com/questions/24832877/android-how-to-highlight-drawable-of-selected-action-bar-tab

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