2013-02-22 42 views
0

我創建標籤是這樣的:TabHost setIndicator - 使用佈局,而不是圖標?

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(選擇器與佈局)?

+0

您在File res/drawable-hdpi/layout_pressed處有資源$ NotFoundException。xml檢查你有這個佈局使用的所有資源 - 圖標,顏色,尺寸 – 2013-02-22 10:49:23

+0

你的logcat顯示在res/drawable-hdpi/tab1_selector.xml文件夾中找不到tab1_selector.xml文件 – 2013-02-22 10:55:44

+0

你可以將tab1_selector.xml文件移動到drawable用於測試的-hdpi文件夾 – 2013-02-22 11:00:38

回答

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方法簡單即可; T爲選擇與佈局,但可以選擇與圖標...

0
  1. 首先把所有selector.xml文件在res /文件夾繪製

更換

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

好了,當然res是局部變量:res = getResources();)那個;沒問題。我所有的選擇器都在可繪製的目錄中,也都在選擇器中使用了佈局。 – qkx 2013-02-22 10:50:16

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

堆棧跟蹤顯示,Android系統無法發現在他們指定的文件夾的一些資源。你有沒有在適當的文件夾中提供resorces。

0

我知道這是舊的,但根據TabHost快速鏈接的API頁面here,如果您想對佈局執行任何操作,請使用setIndicator(View view)而不是setIndicator(CharSequence label, Drawable icon)版本。

基於Drawable APIlayout不能是Drawable。這就像試圖讓橘子變成蘋果。兩者都是水果,就像layouticon是你看到的不僅僅是文本而已。但是,它們仍然不同並且處理方式不同。

相關問題