开发者

android: Set clicked color of Tab to background color of a TableRow

I have a TabLayout. In that I have a tablelayout with dynamically added rows. When a TableRow is selected/clicked I want to set its background color to the Pressing or Clicked color of Tab.

I don't want to set any static color but want to use the default theme color. How to get the color info of Tablayout ?

How to handle the left and right side of the tabs ? This is what I get begore and after setting the tab_background.xml as th backgroundRsourse for all tabs :

android: Set clicked color of Tab to background color of a TableRow

On the left hand side, both the images are without any settings, but the right and left corners are smooth. On right hand side, both images are with setings, see the corners of selected tabs are just a square, they should be rounded off or smoth like the ones without any settings. It seems I got to use tab_selected_bar_right and tab_selected_bar_left. I have copied them to my drawable, but can't make out how do I use them. What settings to give int the xml below provided by @mudit.

开发者_StackOverflow社区

Please guide me. Any help is highly appreciated. I am in a hurry.

Thanks


Add this code after adding the tabs in your java file:

( I am adding only 2 tabs, you can add this if you have more tabs)

TabWidget tw = getTabWidget();

View tab1View = tw.getChildAt(0);
tab1View.setBackgroundResource(R.drawable.tab_background);

View tab2View = tw.getChildAt(1);
tab2View.setBackgroundResource(R.drawable.tab_background);

Code tab_background.xml:

(put this in drawable folder)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="false" android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@drawable/tab_bg" />

    <item android:state_focused="false" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/tab_selected" />

    <item android:state_focused="true" android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@drawable/tab_bg" />

    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/tab_selected" />

    <item android:state_pressed="true"
        android:drawable="@drawable/tab_selected" />
</selector> 


Am solved with this issue. I apology for not mentioning it earlier. Sorry, I just missed out. @Yugandhar, Thanks for your interest.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜