我正在尝试在我的Android应用中实现AppCompat操作栏,但我无法加载样式。它只显示默认的Holo.Light操作栏,而不是我在styles.xml中定义的颜色/样式。我查看了文档,我相信我正在做的一切正确。谁能告诉我我做错了什么?
我将此代码基于GitHub存储库LiveoNavigationBar(https://github.com/rudsonlive/Navigation-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(在值文件夹中,我没有值-v11或值-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'
}
答案 0 :(得分:0)
您可能需要支持库。应该做的伎俩
https://developer.android.com/tools/support-library/setup.html
答案 1 :(得分: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>
此外,如果您想自定义工具栏中的文本和其他ui元素,您可以在布局中使用以下内容:
<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);