Android: 选项卡样式

4
我一直在尝试制作一个类似于这样的选项卡样式。但目前为止没有成功,有人能帮我吗?

http://www.technobuzz.net/wp-content/uploads/2010/02/seesmic-android-260-208.png

该样式只显示所选颜色。当我使用白色图标时,文本(setindicator文本)是白色的。这也适用于灰色图标。

当图标颜色为白色时,setindicator中的文本也会变成白色.. 我该如何解决呢?

谢谢!

Main.java
intent = new Intent().setClass(this, Settings.class);
         spec = tabHost.newTabSpec("settings").setIndicator("Settings",
                res.getDrawable(R.drawable.tab_settings))
                .setContent(intent);
                tabHost.addTab(spec);


                TabWidget tw = getTabWidget(); 
                for (int i = 0; i < tw.getChildCount(); i++) { 
                        View v = tw.getChildAt(i); 
                        v.setBackgroundDrawable(getResources().getDrawable 
                        (R.drawable.custom_tab)); 
                } 

tab_settings

  <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- PRESSED TAB -->
    <item 
        android:state_pressed="true"
        android:drawable="@drawable/artists_on"
        android:color="#bfbfbf"
        />
    <!-- INACTIVE TABS -->
    <item 
        android:state_selected="false"
        android:state_focused="false"
        android:state_pressed="false"
        android:drawable="@drawable/artists_of"
        />
    <!-- ACTIVE TAB -->
    <item 
        android:state_selected="true"
        android:state_focused="false"
        android:state_pressed="false"
        android:drawable="@drawable/artists_of"
        />
    <!-- SELECTED TAB -->
    <item 
        android:state_focused="true"
        android:state_selected="true"
        android:state_pressed="false"
        android:drawable="@drawable/artists_on"
        />

custom_tab.xml 自定义tab风格...

<item android:state_pressed="true" >
    <shape>
        <gradient
            android:startColor="#ea9d32"
            android:endColor="#ffcc50"
            android:angle="270" />
    </shape>
</item>

<!-- WHEN SELECTED --> <!-- HOW CAN I SAID WHEN NOT SELECTED? --> 
    <item android:state_focused="true" >
        <shape>
            <gradient
                android:endColor="#ffcc50"
                android:startColor="#ffcc50"
                android:angle="270" />
        </shape>
    </item>

       <item android:state_focused="false" >
        <shape>
            <gradient
                android:endColor="#ffffff"
                android:startColor="#AAAAAA"
                android:angle="270" />
            <stroke
                android:width="1px"
                android:color="#000000" />
        </shape>
    </item>
</selector>

2
把你尝试过的代码粘贴上来……这样我们就能看到哪里出了问题。 - Cristian
在您的选项卡设置XML中,“artists_on” drawable需要位于活动选项卡选择器中(列表中的第三个),所有其他部分应为“artists_of”。目前,当选项卡被选中但不活动时,您只能看到“artists_on” drawable。 - Josh Clemm
1个回答

2

您的选项卡背景有四种状态,几乎全部都包含了:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item <!-- PRESSED TAB -->
        android:state_pressed="true"
        android:drawable="@drawable/minitab_pressed"
        />
    <item <!-- INACTIVE TABS -->
        android:state_selected="false"
        android:state_focused="false"
        android:state_pressed="false"
        android:drawable="@drawable/minitab_unselected"
        />
    <item <!-- ACTIVE TAB -->
        android:state_selected="true"
        android:state_focused="false"
        android:state_pressed="false"
        android:drawable="@drawable/minitab_default"
        />
    <item <!-- SELECTED TAB -->
        android:state_focused="true"
        android:state_selected="true"
        android:state_pressed="false"
        android:drawable="@drawable/minitab_selected"
        />
</selector>

对于文本颜色,您还需要创建一个选择器并将此可绘制对象分配为 textColor

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@color/white" />
    <item android:state_focused="true" android:color="@color/white" />
    <item android:state_pressed="true" android:color="@color/white" />
    <item android:color="#bfbfbf" />
</selector>

谢谢!所以我为选定的、未选定的和默认的创建了一个(图像)选项卡...我在想我能否更改我的背景?但我认为这不可能实现我想要的..? 我该如何在我的代码中分配textColor选择器..非常感谢您的帮助! - anddevelop
可以为图标、背景和文本设置选择器。我不确定如何在程序中分配textColor,所以我会回到你那里。同时,我建议查看foursquare应用程序及其源代码:http://code.google.com/p/foursquared/source/browse/ - Josh Clemm
没问题,我会组织一个小项目并在明天发布。 - Josh Clemm
@JoshClemm 我应该在哪里创建选择器颜色文件?我需要将其与选项卡选择器一起包含吗? - Hunt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接