在Android中自定义状态选项卡

Chr*_*pix 18 tabs android selector drawable

我知道如何在每个标签上放置图标,这没问题.我也跑过这个:[Stack Overflow线程几乎一样] [1]

我按照该问题的一个链接找到[this] [2]

差不多,它说要使用XML中定义的选择器,当然,这样做了.但是没有与之关联的id,因此我不确定如何将选择器功能作为drawable,因此我可以将其用作选项卡的图标.也许我会以错误的方式解决这个问题.但这就是我所拥有的,显然缺少了一些东西.

<selector
    android:id="@+id/myselector"
    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/darklogo" />
    <item
        android:state_focused="false"
        android:state_selected="true"
        android:state_pressed="false"
        android:drawable="@drawable/lightlogo" />

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

    <!-- Pressed -->
    <item
        android:state_pressed="true"
        android:drawable="@drawable/lightlogo" />
</selector>
Run Code Online (Sandbox Code Playgroud)

在我的代码中,使用以下命令生成示例选项卡:

  host.addTab(host.newTabSpec("three")  
                .setIndicator("map",drawables)  
                .setContent(new Intent(this, Map.class))); 
Run Code Online (Sandbox Code Playgroud)

现在drawable只是对可绘制图像资源的引用.如何使选择器成为可绘制的?

这是我的问题[1]:更新Android标签图标 [2]:http://groups.google.com/group/android-evelopers/browse_thread/thread/ef3bdebcb715b385

Ret*_*ier 21

您在此处包含的XML是一种定义drawable的方法,可以嵌入case语句.它根据分配给它的视图的状态呈现不同的drawable.作为drawable,您应该将其保存为res/drawable项目文件夹中的xml文件(例如tabselector.xml).

要将它用于Tabhost,您需要像平常一样构造TabActivity(如本教程示例所示).

然后,当您将每个选项卡添加到主机时,您将tabselectordrawable 指定为指示符,如下面的"TAB 1"所示.

Drawable mySelector = getResources().getDrawable(R.drawable.tabselector);

mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1", mySelector).setContent(R.id.textview1));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2));
Run Code Online (Sandbox Code Playgroud)

注意:此时您无法更改图标后面的选项卡背景的颜色.