操作栏不接受个人风格

时间:2015-05-26 11:26:42

标签: android android-actionbar android-appcompat android-styles

我正在尝试将我的操作栏变得个性化。我希望它支持API-9及更高版本 问题是,操作栏不接受styles.xml中的更改,并且不显示项目。

这是我的styles.xml:

<resources>

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>

    <style name="AppTheme" parent="AppBaseTheme">

        <item name="android:windowBackground">@color/background_window</item>
        <item name="android:actionBarStyle">@style/ActionBarStyle</item>
    </style>

    <style name="ActionBarStyle" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <!--<item name="android:icon">@drawable/ic_launcher</item>-->
        <item name="android:background">#ff127d08</item>
    </style>

</resources>

应用程序只是显示默认操作栏,没有图标,我选择的项目或背景

2 个答案:

答案 0 :(得分:2)

使用appcompat-v7设置操作栏颜色的官方方法是通过活动主题colorPrimary

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.Apptheme" parent="Theme.AppCompat">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
  </style>
</resources>

关于图标,已多次询问此问题,getSupportActionBar().setDisplayShowHomeEnabled(true);可通过网络搜索找到the answergetSupportActionBar().setIcon(R.drawable.ic_launcher);appcompat icon)。

答案 1 :(得分:1)

转到自定义主题,在res中添加themes.xml - &gt; values文件夹

将此代码用于自定义操作栏

themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="android:actionBarTabStyle">@style/MyActionBarTabs</item>

        <!-- Support library compatibility -->

        <item name="android:actionBarTabTextStyle">@style/MyTheme.ActionBar.TabText</item>
         <item name="android:actionBarTabBarStyle">@style/Divider</item>
    </style>

<!-- for Tab divider -->
<style name="Divider" parent="@android:style/Widget.Holo.ActionBar.TabBar">
    <item name="android:divider">@android:color/transparent</item> //or use your transparent drawable image
    <item name="android:showDividers">middle</item>
    <item name="android:dividerPadding">0dp</item>
</style>

    <!-- ActionBar styles -->
    <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@color/easy</item>
        <!-- Support library compatibility -->
        <item name="background">@color/easy</item>
    </style>

    <style name="MyActionBarTabs" parent="@style/Widget.AppCompat.ActionBar.TabView">
        <item name="android:background">@drawable/actionbar_tab_indicator</item>
    </style>

    <style name="MyTheme.ActionBar.TabText" parent="android:style/Widget.Holo.ActionBar.TabText">

        <!-- This is a Black text color when selected and a WHITE color otherwise -->
        <item name="android:textColor">@color/selector_tab_text</item>
    </style>

</resources>

清单文件调用 - &gt; CustomActionBarTheme

<application
        android:allowBackup="true"
        android:icon="@drawable/easylogo"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/CustomActionBarTheme" >


        <activity
            android:name="com.example.Start"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 </application>
相关问题