我使用此操作栏样式生成器为我的应用程序创建了一个主题。
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>
以下是我的样式文件和 list 文件中的其他内容。
list 文件
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 ,在我看来一切都很好。可能出了什么问题?
最佳答案
What could be wrong?
该工具为 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"/>
关于java - 主题不影响选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19588008/