gpt4 book ai didi

android - 为什么我的自定义 'actionBarTabStyle' 没有覆盖默认样式/主题?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:37:22 25 4
gpt4 key购买 nike

几天来我一直在尝试覆盖自定义标签样式的全息主题,但我的更改没有任何效果。

这是我的 styles.xml

<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme" parent="@android:style/Theme.Holo">
<item name="android:tabWidgetStyle">@style/MyActionBarTabs</item>
</style>

<!-- ActionBar tabs styles -->
<style name="MyActionBarTabs" parent="@android:style/Widget.Holo.ActionBar.TabView">

<!-- tab indicator -->
<item name="android:background">@drawable/tabselector</item>
</style>>

这是我的 tabselector.xml

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

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

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

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

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

</selector>

我在 Activity 中使用 TabHost 添加了选项卡,其布局如下所示

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

<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="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />

<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>

</android.support.v4.app.FragmentTabHost>

我遵循了 Android Developer Page 中的示例

我的标签看起来还是一样的。我的选项卡指示符应该是我自定义的粉红色。然而,没有任何变化,它们仍然是蓝色的。

注意事项:

  1. 我的 list 文件在我的应用程序标签中引用了更新的主题
  2. 我的 styles.xml 在每个值文件夹(values、values-v11、values-v14)中更新
  3. 所有这一切只是在我的应用程序顶部添加一个带有我的 ic_launcher 图像和标题的操作栏

我错过了什么或做错了什么?感谢任何帮助。谢谢大家!

最佳答案

ActionBar 中的选项卡使用与 TabHost 中的选项卡不同的主题。

您需要做的就是将 android:actionBarTabStyle 更改为 android:tabWidgetStyle

完整示例

<style name="Your.Theme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:tabWidgetStyle">@style/Your.TabWidget</item>
</style>

<style name="Your.TabWidget" parent="@android:style/Widget.Holo.TabWidget">
<item name="*android:tabLayout">@layout/your_tab_layout</item>
</style>

<style name="Your.Tab" parent="@android:style/Widget.Holo.ActionBar.TabView">
<item name="android:background">@drawable/your_tab_indicator</item>
<item name="android:layout_width">0dip</item>
<item name="android:layout_weight">1</item>
<item name="android:minWidth">80dip</item>
</style>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Your.Tab"
android:layout_height="?android:attr/actionBarSize"
android:orientation="horizontal" >

<ImageView
android:id="@android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:visibility="gone" />

<TextView
android:id="@android:id/title"
style="@android:style/Widget.Holo.ActionBar.TabText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:maxWidth="180dip" />

</LinearLayout>

不过,android:tabLayout 不是公共(public)属性(因此是 *)。因此,谷歌不建议创建这样的样式,而是建议动态修改指示器。所以,像这样:

    final TabWidget tabWidget = tabHost.getTabWidget();
for (int i = 0; i < tabWidget.getTabCount(); i++) {
final View tab = tabWidget.getChildTabViewAt(i);
tab.setBackground(getResources().getDrawable(R.drawable.your_tab_indicator));
}

结果

Example

关于android - 为什么我的自定义 'actionBarTabStyle' 没有覆盖默认样式/主题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22793652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com