在ICS中不显示的制表符分隔符
我只有在低于冰淇淋三明治的版本中才能使用.setDividerDrawable()。当我运行模拟器时,标签显示完美,但没有分隔线。分频器显示,模拟较低版本的Android时没有问题。在ICS中不显示的制表符分隔符
我正在使用此代码来创建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)是指该图像:这仅仅是一个1px的宽.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背景图像。
我有同样的问题,我终于手动添加分隔符。在标签之间添加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:无法启动活动ComponentInfo {com.sbl.mytabapp/com.sbl.mytabapp.MyTabApp}:java.lang.IllegalStateException:指定的子项已具有父项。您必须先调用子对象的父对象的removeView()。 – Simon 2012-03-07 00:21:13
你是否试图多次添加相同的divider实例?您只能添加一次View(或ImageView)的实例。如果你需要多个divider,你需要创建多个实例。 – 2012-03-07 10:07:21
现货。但是当我尝试用不同的drawable添加一个类似的视图时,视图会混乱起来。我只能按tab1和tab2。 Tab2和Tab3都像Tab2一样。我认为这与意见有关。这个贴图显示了我的代码:[link](http://pastebin.com/z2PDRD8k)第21行和第28行是我添加分隔符的地方。 – Simon 2012-03-07 14:24:51
此行为似乎是ICS选项卡主题中的默认行为,需要解决。看看this answer,特别是@Janusz给出的OP。
我也一直在看这个,但虽然这确实是一个解决方案,但它并不模仿ICS前标签100% – Simon 2012-03-07 00:00:24
你可以试试这段代码!
if(Build.VERSION.SDK_INT >= 11)
mTabHost.getTabWidget().setShowDividers(TabWidget.SHOW_DIVIDER_MIDDLE);
没有足够的信息。什么是R.drawable.divider?标签指标视图的布局XML以及标签本身在哪里? – 2012-03-04 18:14:42
好的,我明白了。我添加了更多信息。希望能帮助到你。 – Simon 2012-03-04 18:49:01