为什么我的Android应用程序在操作栏中不显示图标和文本?

4

我是Android开发的初学者,正在尝试构建我的主题,但遇到了问题。我在Google和d.android上搜索答案,但没有找到。

ActionBar正在改变颜色(这没问题),但应用程序的文本和图标不显示(只有当我使用我的主题时才会出现 - 如果我使用默认主题,则一切正常)。为什么?

res/values/styles.xml:

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

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

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="AppTheme">
    <item name="android:background">HEX COLOR</item>
    <item name="android:titleTextStyle">@style/MyTitleTextStyle</item>
</style>

<style name="MyTitleTextStyle" parent="AppTheme">
    <item name="android:textColor">#FFFFFF</item>
</style>

</resources>

这是我的AndroidManifest.xml文件。
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.appname"
        android:versionCode="1"
        android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

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

        <activity
            android:name="com.example.appname.Intro"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

我有同样的问题。我认为这是由于新版本的 Android Studio 导致的,因为在旧版本中我没有遇到过这个问题,所以我支持你的问题。 - Rick
2个回答

1

"You should add following code in onCreate() method.

getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setIcon(R.drawable.ic_launcher); "


0

我看到你使用AppTheme作为MyActionBar样式的父级,导致循环依赖。MyActionBar -> AppTheme -> MyActionBar ... 你应该使用android:Widget.ActionBar或类似的作为你的操作栏主题的父级。

此外,AppBaseTheme的父级应该是android:Theme.Holo.Light而不是android:Theme.Light

以下是你的res/values/styles.xml文件应该如何显示:

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

    <style name="AppBaseTheme" parent="android:Theme.Holo.Light"/>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="android:Widget.ActionBar">
        <item name="android:background">HEX COLOR</item>
        <item name="android:titleTextStyle">@style/MyTitleTextStyle</item>
    </style>

    <style name="MyTitleTextStyle" parent="AppTheme">
        <item name="android:textColor">#FFFFFF</item>
    </style>

</resources>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接