【问题标题】:Android ActionBar Style Not being appliedAndroid ActionBar 样式未应用
【发布时间】:2015-06-10 15:04:55
【问题描述】:

我一直在尝试更改操作栏中选项卡和菜单的文本颜色。我在这里关注了一些已回答的问题,并查看了 Android 开发者网站。我已经对我的代码进行了更改,但我的 style.xml 文件中的主题没有被应用。

我的清单中指定的 minSdkVersion 是 19,我在棒棒糖设备上运行。

styles.xml

<style name="MyActionBarTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
    <item name="android:actionMenuTextColor">@color/green</item>
</style>

<style name="MyActionBar" parent="android:style/Widget.Holo.ActionBar">
    <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
</style>

<style name="MyActionBarTitleText" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">@color/white</item>
</style>

<style name="MyActionBarTabText" parent="android:style/Widget.Holo.ActionBar.TabText">
    <item name="android:textColor">@color/white</item>
</style>

AndroidManifest.xml

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo.Light">

MainActivity.java

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();

    actionBar.setHomeButtonEnabled(false);

    // Specify that we will be displaying tabs in the action bar.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Set up the ViewPager, attaching the adapter and setting up a listener for when the
    // user swipes between sections.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mAppSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }       

一如既往,非常感谢任何帮助:)

【问题讨论】:

  • 为什么选择 ActionBar?工具栏已经存在一年多了。

标签: android android-actionbar android-styles


【解决方案1】:

AndroidManifest.xml

中使用此代码
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/MyActionBarTheme >

并将此代码用于 Styles.xml

<resources>
    <style name="MyActionBarTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
        <item name="android:actionBarTabTextStyle">@style/tabtextcolor</item>
    </style>

    <style name="MyTheme.ActionBarStyle"parent="@android:style/Widget.Holo.Light.ActionBar">
         <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item
    </style>

    <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">@color/red</item>
    </style>

    <style name="tabtextcolor" parent="@android:style/Widget.Holo.Light.ActionBar.TabText">
        <item name="android:textColor">@android:color/white</item>
    </style>
</resources>

【讨论】:

  • 嗨。这次真是万分感谢。这确实会更改操作栏的主题,但也会导致图标(显示在栏的左上角)停止显示。此外,主题似乎适用于我的片段,即我的列表项中的文本与操作栏无关时正在更改。
  • 嗯,如您所说的更新不会导致使用新样式,它会导致使用默认样式,即与 Holo 相反。不幸的是,这个答案并不能解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-25
  • 1970-01-01
  • 1970-01-01
  • 2015-09-05
相关资源
最近更新 更多