在Android Studio 1.4.1中设置Android工具栏的背景和文本颜色

14

我正在尝试更改活动操作栏的背景颜色和标题颜色,我尝试了官方教程和一些来自这里的答案,但是都没有对我起作用,我缺少某些东西。

styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

样式.xml(v21)

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

MainActivity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton android:id="@+id/fab"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>
1个回答

47

那么,如果你和我一样正在使用Android Studio 1.4.1并且是Android编程的新手,那么设置标题和背景颜色可以按照以下步骤进行:

背景颜色:(检查具有BlanckActivity生成代码的colors.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color><!--Background color for AppBar-->
    <color name="colorPrimaryDark">#303F9F</color><!--i ignore its use-->
    <color name="colorAccent">#FF4081</color><!--color effects, EditText, RadioButtons...-->
</resources>

标题、副标题和弹出菜单及其选项(请查看“styles.xml”):

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:textColorPrimary">TITLE_COLOR_GOES_HERE</item>
        <item name="android:textColorSecondary">SUBTITLE_COLOR_GOES_HERE</item>
</style>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light">
        <item name="android:colorBackground">MENU_POPUP_BACK_COLOR</item>
        <item name="android:textColor">@color/MENU_POPUP_ITEMS_COLOR</item>
</style>

一个活动的主要颜色

我希望这能帮助像我一样在官方 Android 教程中迷惑不解的人。


感谢您的帮助! - LipeTuga

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