ICS中未显示选项卡分隔线

4
我在使用 .setDividerDrawable() 时发现只有在冰淇淋三明治以下版本中才有效。当我运行模拟器时,标签显示得很好,但没有分隔线。在模拟较低版本的Android时,没有任何问题,分隔符显示正常。
我正在使用以下代码创建TabHost。我不知道是什么原因导致ICS出现问题。
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sbl.mytabapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:debuggable="true" >
        <activity
            android:name=".MyTabApp"
            android:label="@string/app_name"
            android:theme="@style/MyTabAppTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Page1"></activity>
        <activity android:name=".Page2"></activity>
        <activity android:name=".Page3"></activity>
    </application>

</manifest>

MyTabApp.java (R.drawable.divider) 引用了这张图片:enter image description here,这是一张只有1像素宽的 .jpg 文件。在 ICS 上不会显示。

public class MyTabApp extends TabActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;

        tabHost.getTabWidget().setDividerDrawable(R.drawable.divider);

        intent = new Intent().setClass(this, Page1.class);
        spec = tabHost.newTabSpec("page1").setIndicator(getLayoutInflater().inflate(R.layout.tab1, null))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Page2.class);
        spec = tabHost.newTabSpec("page2").setIndicator(getLayoutInflater().inflate(R.layout.tab2, null))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Page3.class);
        spec = tabHost.newTabSpec("page3").setIndicator(getLayoutInflater().inflate(R.layout.tab3, null))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);

    } 
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</TabHost>

style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

 <style name="MyTabAppTheme" parent="android:style/Theme"> 
    <item name="android:windowNoTitle">true</item>
 </style> 


 <style name="tablayout" parent="android:style/Theme">
     <item name="android:layout_width">match_parent</item>
     <item name="android:layout_height">match_parent</item>
     <item name="android:height">48dp</item>
     <item name="android:gravity">center</item>
     <item name="android:textColor">@color/font</item>
     <item name="android:background">@drawable/tabselector</item>
 </style>

 <style name="contentlayout" parent="android:style/Theme">
     <item name="android:layout_width">match_parent</item>
     <item name="android:layout_height">match_parent</item>
     <item name="android:textColor">@color/font</item>
     <item name="android:background">@color/background</item>
 </style>
</resources>

tab1.xml、tab2.xml和tab3.xml都包含相同的引用样式。这是选项卡1:

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

tabselector.xml选项卡的背景是9patch背景图像。

<?xml version="1.0" encoding="utf-8"?>
<selector 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/normal" />
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/selected" />

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

    <!-- Pressed -->
    <item android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/normal_pressed" />
    <item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/selected_pressed" />
</selector> 

外观如何:选项卡背景是9patch背景图像。
enter image description here

信息不足。R.drawable.divider是什么?选项卡指示器视图和选项卡本身的布局XML在哪里? - Reuben Scratton
好的,我明白了。我添加了更多信息。希望能有所帮助。 - Simon
3个回答

4

我有同样的问题,最终我手动添加了分隔符。在标签之间添加ImageView...

public class MyTabApp extends TabActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView divider = new ImageView(this);
        divider.setImageResource(R.drawable.tab_seperator);

        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;

        intent = new Intent().setClass(this, Page1.class);
        spec = tabHost.newTabSpec("page1").setIndicator(getLayoutInflater().inflate(R.layout.tab1, null))
                  .setContent(intent);
        tabHost.addTab(spec);

        tabHost.getTabWidget().addView(divider, LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);

        intent = new Intent().setClass(this, Page2.class);
        spec = tabHost.newTabSpec("page2").setIndicator(getLayoutInflater().inflate(R.layout.tab2, null))
                  .setContent(intent);
        tabHost.addTab(spec);
    } 
}

我遇到了这个错误,你能帮我修复吗: E/AndroidRuntime(683): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sbl.mytabapp/com.sbl.mytabapp.MyTabApp}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. - Simon
1
你是否试图多次添加相同的分隔线实例?你只能添加一个视图(或ImageView)实例一次。如果你需要多个分隔线,你需要创建多个实例。 - Johan B
1
很准确。但是当我尝试添加一个带有不同drawable的类似视图时,视图混乱了。我只能按tab1和tab2。 Tab2和tab3都像tab2一样操作。我认为这与视图有关。这个粘贴展示了我的代码:[链接](http://pastebin.com/z2PDRD8k)第21行和28行是我添加分隔符的地方。 - Simon
我猜这是因为视图的索引已经改变了。实际上,我的选项卡上有onClickListeners,并手动调用setCurrrentTab,这是因为其他原因,但这可能是为什么它对我有效的原因。(我很快会粘贴一些代码) - Johan B
1
可能会有这样的效果:http://pastebin.com/MHALzj65(我实际上没有测试过代码,因为它不完全符合我的应用方式,但我认为它应该可以工作。) - Johan B

1
你可以尝试这段代码!
if(Build.VERSION.SDK_INT >= 11)
    mTabHost.getTabWidget().setShowDividers(TabWidget.SHOW_DIVIDER_MIDDLE);

1

这种行为似乎是ICS选项卡主题中的默认行为,需要一个解决方法。请查看this answer,特别是由@Janusz提供的答案。


我也一直在研究这个问题,虽然这确实是一个解决方案,但它并不能完全模仿ICS之前的选项卡。 - Simon

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