2010-11-08 47 views
14

我使用的标签在我的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); 
     } 
    }); 
} 
+0

相关,但不完全重复,但有更好的(更新?)答案如何风格的TabWidget:http://stackoverflow.com/a/2805256/2291 – 2015-12-22 17:56:40

回答

-6

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

<!-- Pressed --> 
<item android:state_pressed="true" android:drawable="@drawable/tab_press" /> 

+9

这是如何回答我的问题? – gizero 2011-02-16 21:44:06

+1

@gizero这是怎么接受的答案呢? :) – hendrix 2013-02-01 13:58:06

+0

因为这是你如何设计一个选项卡。 – Ajibola 2013-02-24 17:07:46

11

这家伙已经发布,可以自定义有关选项卡都漂亮多了。适用于Android 1.6及以上版本:Custom Android Tabs

我还没有尝试过,但到达那里。基本上你可以在你的TabSpec上调用setIndicator,并将它传递给一个视图,而不仅仅是文本。然后你就可以。如果你还在使用上> 14 TabWidgets使用选择等

+0

但遗憾的是你仍然需要至少分钟sdk4。 – Fraggle 2011-03-10 18:03:34

+34

“这个家伙”就是我。 – 2013-04-19 02:03:49

2

自定义视图,看到http://code.google.com/p/android/issues/detail?id=2267。最后评论指向iosched2011也的确有他们更换了TabIndicator整个视图。除了一些属性(一般背景,文本)外,任何简单的方式似乎都被破坏了,并且这种复杂性是必需的。