AppCompatActionBar无法从style.xml加载样式。

问题描述 投票:0回答:2

我试图在我的Android应用中实现AppCompat动作条,但我无法加载该样式。它只是显示默认的Holo.Light actionbar,而不是采用我在style.xml中定义的颜色样式。我看了文档,我相信我做的一切都是正确的。谁能告诉我我做错了什么?

我的这段代码是基于 GitHub 仓库 LiveoNavigationBar (https:/github.comrudsonliveNavigation-Drawer-ActionBarCompat。)除了我是为Android API 15+写的这个应用。

这是我的AndroidManifest.xml。

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

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

    <activity
        android:name=".main.ActivityMain"
        android:label="@string/app_name"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".login.ActivityLogin"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustResize|stateVisible" >
    </activity>

    <activity
        android:name=".settings.ActivitySettings"
        android:label="@string/title_activity_settings"
        android:parentActivityName=".main.ActivityMain" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".main.ActivityMain" />
    </activity>

</application>

这是我的styles.xml(在values文件夹内,我没有values-v11或values-v14,因为我的应用是针对API 15+)。

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

<!--
    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.Holo.Light.DarkActionBar">
    <!-- API 14 theme customizations 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>

<style name="ie2oActionBarTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarStyle">@style/ie2oActionBar</item>
</style>

<style name="ie2oActionBar" parent="@style/Widget.AppCompat.ActionBar">
    <item name="titleTextStyle">@style/ie2o.TitleTextStyle</item>
    <item name="subtitleTextStyle">@style/ie2o.SubTitleTextStyle</item>
    <item name="background">@color/blue</item>
</style>

<style name="ie2o.TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/white</item>
    <item name="android:textStyle">normal</item>
</style>

<style name="ie2o.SubTitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle">
    <item name="android:textColor">@color/white</item>
    <item name="android:textStyle">normal</item>
</style>

<!-- Style user drawer -->
<style name="text_view_title_list" parent="android:Widget.TextView">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">@drawable/drawer_bg_black_transparent</item>
    <item name="android:ellipsize">end</item>
    <item name="android:padding">4dp</item>
    <item name="android:layout_gravity">bottom</item>
</style>

</resources>

下面是我的build.gradle文件

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.1"

    defaultConfig {
        applicationId "net.ie2o.appname"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:21.0.+' // (I have tried it with and without this line, the code I referenced has this line, but the documentation does not)
    compile 'com.android.support:appcompat-v7:21.0.+'
    compile 'com.google.android.gms:play-services:6.5.87'
}
android android-support-library android-toolbar android-appcompat
2个回答
0
投票

你必须改变你的风格,使用Theme.AppCompat主题(Light,DartActionBar,NoActionBar...)。

 <!-- Base application theme. -->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">   

    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">@color/primary</item>

    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">@color/primary_dark</item>

    <!-- colorAccent is used as the default value for colorControlActivated
             which is used to tint widgets -->
    <item name="colorAccent">@color/accent</item>
</style>

另外,如果你想自定义工具栏内的文本和其他用户界面元素,你可以在你的布局中使用这样的东西。

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ActionBarThemeOverlay"
        android:background="?attr/colorPrimary" />

为你的工具栏定义主题。

  <style name="ActionBarThemeOverlay" parent="">
        <item name="android:textColorPrimary">#fff</item>
        <item name="colorControlNormal">#fff</item>
        <item name="colorControlHighlight">#3fff</item>
    </style>

最后在你的活动中

Toolbar toolbar = (Toolbar) findViewByid(R.id.toolbar);
setSupportActionBar(mActionBarToolbar);

0
投票

你可能需要支持库。应该可以做到这一点

https:/developer.android.comtoolssupport-librarysetup.html。

© www.soinside.com 2019 - 2024. All rights reserved.