我使用的標籤在我的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