【问题标题】:Tab Dividers Not Showing In ICS选项卡分隔线未显示在 ICS 中
【发布时间】:2012-03-01 19:57:59
【问题描述】:

我对 .setDividerDrawable() 有一个问题,它只能在低于 Ice Cream Sandwich 的版本中工作。当我运行模拟器时,选项卡显示完美,但没有分隔线。在模拟较低版本的 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) 指的是这张图片:,它只是一个 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 背景图片。

【问题讨论】:

  • 信息不足。什么是 R.drawable.divider?选项卡指示器视图和选项卡本身的布局 XML 在哪里?
  • 好的,我明白了。我添加了更多信息。希望对您有所帮助。

标签: android android-tabhost android-4.0-ice-cream-sandwich


【解决方案1】:

我遇到了同样的问题,最后我还是手动添加了分隔符。在选项卡之间添加 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:指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()。
  • 您是否要多次添加相同的分隔符实例?您只能添加一次视图(或 ImageView)的实例。如果您需要多个分隔线,则需要创建多个实例。
  • 正确。但是,当我尝试使用不同的可绘制对象添加类似的视图时,视图就会混乱。我只能按tab1和tab2。 Tab2 和 tab3 都像 tab2 一样。我认为这与观点有关。此粘贴显示我的代码:link 第 21 和 28 行是我添加分隔符的位置。
  • 我猜这是因为视图的索引发生了变化。实际上,我的选项卡上有 onClickListeners 并手动调用 setCurrrentTab ,这是我这样做的,因为出于其他原因,但这可能是它对我有用的原因。 (我将很快粘贴一些代码)
  • 这样的事情可能会起作用:pastebin.com/MHALzj65(我还没有实际测试过代码,因为它不是我应用它的方式,但我认为它应该可以工作。)
【解决方案2】:

此行为似乎是 ICS 选项卡主题中的默认行为,需要解决。看看this answer,特别是OP 的@Janusz 给出的那个。

【讨论】:

  • 我也一直在研究这个,虽然这确实是一个解决方案,但它并没有 100% 模仿前 ICS 选项卡
【解决方案3】:

你可以试试这个代码!

if(Build.VERSION.SDK_INT >= 11)
    mTabHost.getTabWidget().setShowDividers(TabWidget.SHOW_DIVIDER_MIDDLE);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 2014-08-11
    • 1970-01-01
    • 2020-05-14
    • 2011-03-18
    • 1970-01-01
    相关资源
    最近更新 更多