无法使用Theme.AppCompat

时间:2015-09-07 14:12:48

标签: android android-actionbar android-theme

我是Android的新手,我完全遵循Android官方教程,直到这一点:https://developer.android.com/training/basics/actionbar/styling.html我正在尝试更改背景颜色并使用操作栏查看操作栏上的默认Android图标支持库(使用Theme.AppCompact)。但是,当我尝试设置像#AA0000这样的十六进制颜色时,Android Studio给出了错误“无法解析符号”(在themes.xml中的支持库行),如果没有错误我也看不到Android操作栏左侧的图标。

期望如下:

enter image description here

真实结果(当我删除十六进制并设置背景图像以避免错误时)如:

enter image description here

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

但是,在这种情况下,我只能设置背景图像,而不是十六进制颜色,我无法查看图标,这些是我的文件(在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>

的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>

    <!-- 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>

MyActivity.java

public class MyActivity extends AppCompatActivity {
    public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        // Handle presses on the action bar items
        switch (id) {
            case R.id.action_search:
                //openSearch();
                Log.i("MyActivity", "SEARCH PRESSED");
                return true;
            case R.id.action_settings:
                //openSettings();
                Log.i("MyActivity", "SETTINGS PRESSED");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    /** Called when the user clicks the Send button */
    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
}

activity_my.xml

<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>

** 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

<resources>

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

</resources>

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

<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>

这不起作用,因为如果你想使用它,你需要定义颜色并命名它。

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

<item name="my_color" type="color">#AA0000</item>

然后通过其资源名称引用它来使用颜色:

<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>

有关颜色的更多信息:https://developer.android.com/samples/BasicMediaRouter/res/values/colors.html

此外,使用AppCompatActivity时,您不会在操作栏中看到应用程序图标。这只是该类的默认行为。但如果你想看到它:show icon in actionbar/toolbar with AppCompat-v7 21

答案 1 :(得分:0)

这项工作对我来说:

<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>

注意tools:ignore="NewApi"属性