Android操作栏,更改颜色并添加搜索功能

时间:2014-12-23 07:04:53

标签: java android xml android-actionbar

您能帮我更改操作栏的颜色以及如何添加搜索功能(ic_search菜单)

我正在使用API​​ 14,我还将库v7附加到我的项目

我试过这个颜色,但它不起作用? 我的样式文件

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <item name="android:windowFullscreen">true</item>
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>






        <!-- ActionBar styles -->
        <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
             <item name="android:background">@color/orange_dark</item>
        </style>



</resources>

我的清单:

  <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" />


    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" android:name=".gears.MyApplication">

        <activity
            android:name=".SplashScreen"
            android:label="@string/title_activity_splash_screen" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
        </activity>





    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

为了改变颜色,你应该在你的values文件夹中创建一个styles.xml,如下所示:

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

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

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

        <!-- Support library compatibility -->
        <item name="background">@color/action_bar</item>
        <item name="backgroundSplit">@color/action_bar</item>
    </style>



</resources>

在您的清单文件中,在应用程序代码或活动代码中添加自定义主题:

<application
android:name=".MendiLineApplication"
android:allowBackup="true"
android:icon="@drawable/blue_mountain_b"
android:label="@string/app_name"
android:theme="@style/CustomActionBarTheme" >

要将搜索图标添加为操作,请在菜单配置文件(menu.xml)中添加一个项目:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/search"
        android:icon="@drawable/ic_action_search"
          android:title="Search"
          android:showAsAction="ifRoom"
    />
</menu>