我使用的标签在我的Android应用程序,我已经运行在HTC Sense的手机应用程序时,遇到这个问题自定义样式: targetSdkVersion为4)并不能解决这个问题,我也不想将目标sdk设置为4.对Android的TabWidget
我反而尝试解决这个问题,我创建了自己的选项卡小部件样式,并修改了文本颜色。问题是,当我使用我自己的风格时,没有什么明显的区别。即样式似乎不适用于标签。
这是主要活动代码,拿着标签:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab 1").setContent(new Intent(this, Tab1.class)));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab 2").setContent(new Intent(this, Tab2.class)));
tabHost.setCurrentTab(0);
}
这是我的tab.xml。我已指定MyTabStyle的风格TabWidget注意:
<?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="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
style="@style/MyTabStyle"
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
这是我MyTabStyle的定义,这是我在res /价值/ styles.xml定义:
<style name="MyTabStyle" parent="@android:style/TextAppearance.Widget.TabWidget">
<item name="android:textColor">#5DFC0A</item>
</style>
为什么没有变化在MyTabStyle中显示在我的应用程序?任何其他解决方案来解决HTC Sense中选定选项卡上的不可见文本?
UPDATE 2011-06-02
我设法解决这个问题的排序哈克的方式,通过知识选项卡上的文字实际上是TextViews。下面的方法添加到您的活动:
private void setTabColor(TabHost tabHost) {
try {
for (int i=0; i < tabHost.getTabWidget().getChildCount();i++) {
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
if (tv != null) {
tv.setTextColor(Color.parseColor("#ffffff"));
}
TextView tv2 = (TextView) tabHost.getCurrentTabView().findViewById(android.R.id.title); // Selected Tab
if (tv2 != null) {
tv2.setTextColor(Color.parseColor("#000000"));
}
}
} catch (ClassCastException e) {
// A precaution, in case Google changes from a TextView on the tabs.
}
}
添加以下在onCreate()方法在你的活动:
// Only apply the fix if this is a HTC device.
if (android.os.Build.MANUFACTURER.toLowerCase().contains("htc")) {
// Set up the color initially
setTabColor(tabHost);
// Add a tab change listener, which calls a method that sets the text color.
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(tabHost.getApplicationWindowToken(), 0);
setTabColor(tabHost);
}
});
}
相关,但不完全重复,但有更好的(更新?)答案如何风格的TabWidget:http://stackoverflow.com/a/2805256/2291 – 2015-12-22 17:56:40