在Android中自定义状态标签 [英] Customizing tabs on state in Android

查看:80
本文介绍了在Android中自定义状态标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在每个选项卡上放置图标,这没问题.我也遇到了这个问题: [在几乎相同的东西上发生堆栈溢出线程] [1]

I know how to put the icon on each tab, that is no problem. I also ran across this : [Stack Overflow thread on pretty much same thing][1]

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

I followed one of the links from that question and found [this][2]

几乎,它说使用XML中定义的选择器确实可以做到这一点.但是没有与它关联的id,所以我不确定如何将选择器功能作为可绘制对象使用,因此我可以将其用作选项卡的图标.也许我正在以错误的方式来解决这个问题.但这就是我所拥有的,而且显然缺少一些东西.

Pretty much, it said to use a selector defined in the XML, sure, did that. But there is no id associated w/ it so I am not sure how to get the selector function as a drawable so I can use it as the icon for the tabs. Maybe I am going about this the wrong way. But this is what I have, and obviously missing something.

<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>

在我的代码中,示例标签是使用生成的:

In my code, an example tab is generated using :

  host.addTab(host.newTabSpec("three")  
                .setIndicator("map",drawables)  
                .setContent(new Intent(this, Map.class))); 

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

Right now drawable is just a reference to a drawable image resource. How do I make the selector a drawable?

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

This is my question [1]: Updating Android Tab Icons [2]: http://groups.google.com/group/android-evelopers/browse_thread/thread/ef3bdebcb715b385

推荐答案

此处包含的XML是一种定义可绘制的方法,可让您嵌入case语句.根据要分配给其的View的状态,它呈现不同的可绘制对象.作为可绘制对象,应将其另存为项目的res/drawable文件夹中的xml文件(例如tabselector.xml).

The XML you've included here is a way of defining a drawable that lets you embed a case statement. It presents a different drawable depending on the state of the View it's being assigned to. As a drawable, you should save it as an xml file within the res/drawable folder of your project (for example tabselector.xml).

要将其用于Tabhost,您需要像往常一样构造TabActivity(如此

To use it for the Tabhost, you need to construct the TabActivity as you normally would (as shown in this tutorial example).

然后,当将每个选项卡添加到主机时,指定tabselector可绘制对象作为指示符,如下面的"TAB 1"所示.

Then when you add each tab to the host, you specify the tabselector drawable as the indicator as shown for "TAB 1" below.

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));

注意:此时,您不能更改图标后面的标签背景的颜色.

Note: You cannot change the color of the tab backgrounds behind the icons at this point.

这篇关于在Android中自定义状态标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆