0

我正在以这种方式创建标签:

TabSpec tab = tabHost.newTabSpec("Tab1").setIndicator("Tab1",res.getDrawable(R.drawable.tab1_selector));

这是我的选择器:

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

    <item android:drawable="@drawable/icon_pressed" android:state_selected="true"/>
    <item android:drawable="@drawable/icon_unpressed"/>

</selector>

其中icon_pressedicon_unpressed只是图标(png)。这工作正常。但是,我想使用而不是设置图标和标题来设置布局(我可以在其中设置我的标题+我想要的图标)。所以我尝试了这个:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tab3Selector">    
    <item android:drawable="@drawable/layout_pressed" android:state_selected="true"/>
    <item android:drawable="@drawable/layout_unpressed"/>    
</selector>

其中layout_pressedlayout_unpressed是这样的布局:

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon_pressed" 
        android:layout_alignParentLeft="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="35dp"
        android:textColor="@color/red"
        android:textSize="@dimen/fontSmall"/>

</RelativeLayout>

奇怪的是我在运行时我的应用程序抛出错误:

    89): FATAL EXCEPTION: main
02-22 11:22:40.422: E/AndroidRuntime(9089): java.lang.RuntimeException: Unable to start activity ComponentInfo {com.test.myapp/com.test.myapp.activities.TabsActivity}: android.content.res.Resources$NotFoundException: File res/drawable-hdpi/tab3_selector.xml from drawable resource ID #0x7f020010
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.app.ActivityThread.access$600(ActivityThread.java:142)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.os.Looper.loop(Looper.java:137)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.app.ActivityThread.main(ActivityThread.java:4931)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at java.lang.reflect.Method.invokeNative(Native Method)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at java.lang.reflect.Method.invoke(Method.java:511)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at dalvik.system.NativeStart.main(Native Method)
02-22 11:22:40.422: E/AndroidRuntime(9089): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-hdpi/tab1_selector.xml from drawable resource ID #0x7f020010
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.content.res.Resources.loadDrawable(Resources.java:1947)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.content.res.Resources.getDrawable(Resources.java:659)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at com.test.myapp.activities.TabsActivity.onCreate(TabsActivity.java:39)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.app.Activity.performCreate(Activity.java:5008)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2139)
02-22 11:22:40.422: E/AndroidRuntime(9089):     ... 11 more
02-22 11:22:40.422: E/AndroidRuntime(9089): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-
hdpi/layout_pressed.xml from drawable resource ID #0x7f020016
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.content.res.Resources.loadDrawable(Resources.java:1947)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.content.res.Resources.getDrawable(Resources.java:659)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:173)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:881)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.content.res.Resources.loadDrawable(Resources.java:1944)
02-22 11:22:40.422: E/AndroidRuntime(9089):     ... 16 more
02-22 11:22:40.422: E/AndroidRuntime(9089): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid drawable tag RelativeLayout
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:877)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
02-22 11:22:40.422: E/AndroidRuntime(9089):     at android.content.res.Resources.loadDrawable(Resources.java:1944)
02-22 11:22:40.422: E/AndroidRuntime(9089):     ... 21 more

这是什么意思,我不能在 TabHost 选项卡中使用 setIndicator(带布局的选择器)?

4

4 回答 4

0
  1. 首先将所有 selector.xml 文件放在 res/drawable 文件夹中

代替

TabSpec tab = tabHost.newTabSpec("Tab1").setIndicator("Tab1",res.getDrawable(R.drawable.tab1_selector));

TabSpec tab = tabHost.newTabSpec("Tab1").setIndicator("Tab1",getResources().getDrawable(R.drawable.tab1_selector));
于 2013-02-22T10:45:15.237 回答
0

Caused by: android.content.res.Resources$NotFoundException: File res/drawable-hdpi/tab1_selector.xml from drawable resource ID #0x7f020010



Caused by: android.content.res.Resources$NotFoundException: File res/drawable-hdpi/layout_pressed.xml from drawable resource ID #0x7f020010

Stacktrace 显示 Android 系统在其指定文件夹中找不到某些资源。您是否在适当的文件夹中提供了资源。

于 2013-02-22T10:57:03.053 回答
0

好吧,这可能不是我的错,而是android问题。看看我的选择器:

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

    <!-- Layouts (in drawable folder) - don't work !!!  -->

    <item android:drawable="@drawable/pressed_layout" android:state_selected="true"/>
    <item android:drawable="@drawable/unpressed_layout" android:state_selected="false"/>

    <!-- Icons (in drawable folder) - work !!!  -->

<!--<item android:drawable="@drawable/pressed_icon" android:state_selected="true"/> -->
<!--<item android:drawable="@drawable/unpressed_icon"/> -->

</selector>

看起来在 TabHost 的 setIndicator 方法中很简单;不能是带有布局的选择器,但可以是带有图标的选择器......

于 2013-02-22T11:49:11.953 回答
0

我知道这很旧,但根据此处TabHost快速链接的 API 页面,如果您想对布局做任何事情,请使用版本而不是版本。setIndicator(View view)setIndicator(CharSequence label, Drawable icon)

基于Drawable API,alayout不能是Drawable. 这就像试图让一个橙子变成一个苹果。两者都是水果,就像 alayout和 anicon都是你认为可能不仅仅是文本的东西。但是,它们仍然不同,处理方式也不同。

于 2014-03-25T21:19:50.933 回答