Android Can';t设置十六进制颜色并使用Theme.AppCompat查看操作栏上的图标

Android Can';t设置十六进制颜色并使用Theme.AppCompat查看操作栏上的图标,android,android-actionbar,android-theme,Android,Android Actionbar,Android Theme,我是Android新手,在这之前我一直严格遵循Android官方教程:我正在尝试更改背景颜色,并使用支持库(主题为.AppCompact)查看操作栏上的默认Android图标。但是当我尝试设置像#AA0000这样的十六进制颜色时,Android Studio会给我一个错误“无法解析符号”(在themes.xml中的Support Library行上),如果没有错误,我也看不到操作栏左侧的Android图标 期望是这样的: 实际结果(当我移除十六进制并设置背景图像以避免错误时)如下所示: 左边

我是Android新手,在这之前我一直严格遵循Android官方教程:我正在尝试更改背景颜色,并使用支持库(主题为.AppCompact)查看操作栏上的默认Android图标。但是当我尝试设置像#AA0000这样的十六进制颜色时,Android Studio会给我一个错误“无法解析符号”(在themes.xml中的Support Library行上),如果没有错误,我也看不到操作栏左侧的Android图标

期望是这样的:

实际结果(当我移除十六进制并设置背景图像以避免错误时)如下所示:

左边的图标丢失了(还有右边的菜单底部,但我不知道问题是否与屏幕的英寸尺寸有关)

但是,在这种情况下,我只能设置背景图像,不能设置十六进制颜色,并且我无法查看图标,这些是我的文件(在themes.xml上使用十六进制颜色):

这是AndroidManifest.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myfirstapp" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MyActivity"
        android:label="@string/app_name"
        android:theme="@style/CustomActionBarTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName=".MyActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.mycompany.myfirstapp.MyActivity" />
    </activity>
</application>
<uses-sdk android:minSdkVersion="14" />

</manifest>
<?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>

    <!-- 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">#AA0000</item>

    <!-- Support library compatibility -->
    <item name="background">#AA0000</item>
</style>
</resources>
<LinearLayout 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:orientation="horizontal" >

<EditText android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage"/>

</LinearLayout>
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

</resources>
活动\u my.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myfirstapp" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MyActivity"
        android:label="@string/app_name"
        android:theme="@style/CustomActionBarTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName=".MyActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.mycompany.myfirstapp.MyActivity" />
    </activity>
</application>
<uses-sdk android:minSdkVersion="14" />

</manifest>
<?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>

    <!-- 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">#AA0000</item>

    <!-- Support library compatibility -->
    <item name="background">#AA0000</item>
</style>
</resources>
<LinearLayout 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:orientation="horizontal" >

<EditText android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage"/>

</LinearLayout>
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

</resources>

**main_activity_actions.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    android:title="@string/action_search"
    app:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    app:showAsAction="never" />
</menu>

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myfirstapp" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MyActivity"
        android:label="@string/app_name"
        android:theme="@style/CustomActionBarTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName=".MyActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.mycompany.myfirstapp.MyActivity" />
    </activity>
</application>
<uses-sdk android:minSdkVersion="14" />

</manifest>
<?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>

    <!-- 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">#AA0000</item>

    <!-- Support library compatibility -->
    <item name="background">#AA0000</item>
</style>
</resources>
<LinearLayout 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:orientation="horizontal" >

<EditText android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage"/>

</LinearLayout>
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

</resources>

我怎样才能解决这个问题?

这对我来说很有用:

<resources xmlns:tools="http://schemas.android.com/tools">

    <style name="BuffTheme" parent="@style/Theme.AppCompat.Light">
        <item name="android:actionBarStyle" tools:ignore="NewApi">@style/MyActionBar</item>
        <item name="actionBarStyle">@style/MyActionBar</item>

    </style>

    <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background" tools:ignore="NewApi">@color/dark_action_bar</item>
        <item name="background">@color/dark_action_bar</item>
        <item name="titleTextStyle">@style/TitleTextStyle</item>
        <item name="subtitleTextStyle">@style/SubTitleTextStyle</item>
    </style>

    <style name="TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/white</item>
    </style>
    <style name="SubTitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/white</item>
        <item name="android:textSize">11sp</item>
    </style>

</resources>

@样式/MyActionBar
@样式/MyActionBar
@颜色/深色\u动作\u栏
@颜色/深色\u动作\u栏
@样式/标题文本样式
@样式/副标题文本样式
@颜色/白色
@颜色/白色
11便士
注意
工具:ignore=“NewApi”
属性
<style name="MyActionBar"
    parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">#AA0000</item>

    <!-- Support library compatibility -->
    <item name="background">#AA0000</item>
</style>
</resources>
#AA0000 #AA0000 这不起作用,因为如果要使用颜色,需要定义颜色并命名

在res/values文件夹中,应该有一个名为colors.xml的文件。如果没有,请在res/values文件夹中创建colors.xml,然后将颜色定义添加到colors.xml:

<item name="my_color" type="color">#AA0000</item>
#AA0000
然后通过引用其资源名称来使用颜色:

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

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

@颜色/我的颜色
@颜色/我的颜色
有关颜色的更多信息:


此外,在使用AppCompatActivity时,您不会在操作栏中看到应用程序图标。这只是该类的默认行为。但是,如果您想查看它:

请检查使用AppCompatActivity时是否有帮助,默认情况下您不会看到应用程序图标。但是如果你想看,试试这个,我必须插入这两行才能看到应用图标?getSupportActionBar().setDisplayShowHomeEnabled(true);getSupportActionBar().setIcon(R.drawable.ic_启动器)@MattC将这些行添加到活动的
onCreate
方法中。