2013-10-25 49 views
0

我使用此操作栏样式生成器为我的应用程序创建了一个主题。不影响标签的主题

http://jgilfelt.github.io/android-actionbarstylegenerator/

我已经设置了标签颜色以橙色,但它并不影响我的布局选项卡。

我的布局看起来如下。

<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@android:id/tabhost" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0" 
      android:orientation="horizontal" /> 

     <FrameLayout 
      android:id="@+id/tabFrameLayout" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</android.support.v4.app.FragmentTabHost> 

下面是我在我的样式文件和清单文件中的其他内容。

清单文件

android:theme="@style/Theme.Silence" 

styles_silence.xml

<style name="Theme.Silence" parent="android:Theme.Holo.Light"> 
    <item name="android:actionBarTabStyle">@style/ActionBarTabStyle.Silence</item> 
    ... 
</style> 
... 
<style name="ActionBarTabStyle.Silence" parent="@android:style/Widget.Holo.Light.ActionBar.TabView"> 
    <item name="android:background">@drawable/tab_indicator_ab_silence</item> 
</style> 

tab_indicator_ab_silence.xml

(这是由我先前链接的程序生成)
<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="@android:color/transparent" /> 
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_silence" /> 

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

    <!-- Pressed --> 
    <!-- Non focused states --> 
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_silence" /> 
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_silence" /> 

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

我查了this document,在我看来,一切都好。什么可能是错的?

回答

4

出了什么问题?

该工具生成存在于ActionBar标签的样式,但你使用普通的标签(通过标准TabWidget),其中有一个与之相关的不同的风格。该风格在主题中由tabWidgetStyle属性表示,该属性指向样式Widget.(Holo).TabWidget风格。

不幸的是,通过Widget.TabWidget风格的主题,您不能更改背景(并且作为管理该风格的属性的标签布局都没有隐藏),但您可以通过将新布局分配为标签指示器来手动执行此操作,把你的选择在那里:

fragmentTabHost.addTab(fragmentTabHost.newTabSpec("tabText").setIndicator(buildTabLayout("tabText")), YourFragmentClass.class, null); 

其中buildTabLayout()是这样的方法:

private View buildTabLayout(String tag) { 
    View tab = getLayoutInflater().inflate(R.layout.new_tab_layout, null);  
    return tab; 
} 

的布局是:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:background="@drawable/tab_indicator_ab_silence" // or you could have a style attribute 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" android:text="simple"/> 

+0

谢谢!这也适用,但有点奇怪。你提到了ActionBar的方式。你的意思是[这个](http://developer.android.com/guide/topics/ui/actionbar.html#Tabs)?如果我没有完全误解,它可以与支持库一起使用API​​ 10。我对么? – MikkoP

+0

@MikkoP我已经讲过'ActionBar',提到样式不匹配。 'ActionBar'对tabs视图和控制器有不同的实现,它不会**使用'TabHost'(以及一个'TabWidget'),所以样式也会不同。你发布的链接指的是ActionBar的选项卡(你确实可以实现较低的API版本,但这不重要,因为你正在处理**内容视图**中的一个“TabHost”部分)该应用程序)。 – Luksprog

+0

你是什么意思?“但是这并不重要,因为你正在处理应用程序的内容视图部分中的TabHost”?我想我是否用这个替换了我的TabWidget。 – MikkoP

1

复制粘贴以下RES /值-V14/styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android"> 

    <!-- 
    Base application theme for API 14+. This theme completely replaces 
    AppBaseTheme from BOTH res/values/styles.xml and 
    res/values-v11/styles.xml on API 14+ devices. 
    --> 
    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar"> 
     <!-- API 14 theme customizations can go here. --> 
     <item name="android:actionBarStyle">@style/MyActionBar</item><!-- your theme name --> 
     <item name="android:actionBarDivider">@null</item> 
    </style> 

    <!-- style for the action bar backgrounds --> 
    <style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar"> 
     <item name="android:backgroundStacked">@drawable/tab_bar_bg</item> <!-- change your image --> 
     <item name="android:titleTextStyle">@style/MyActionBar.Text</item> <!-- to cusomize the font style on tab bar --. 
    </style> 

    <style name="MyActionBar.Text" parent="@android:style/TextAppearance"> 
     <item name="android:textColor">#A3A3A3</item> 
     <item name="android:textSize">15sp</item> 
    </style> 

</resources> 

更改默认主题的主题在AndroidManifest.xml

<application 
     android:theme="@style/AppTheme" > 
     ---- 
     ---- 
    </application>