【问题标题】:Setting Android Toolbar Background and Text Colors in Android Studio 1.4.1在 Android Studio 1.4.1 中设置 Android 工具栏背景和文本颜色
【发布时间】:2015-11-11 17:08:51
【问题描述】:

我正在尝试更改活动操作栏的背景颜色和标题颜色,我尝试了官方教程和这里的一些答案,但没有一个对我有用,我错过了一些东西。

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

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

主要活动:

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

【问题讨论】:

    标签: android android-studio android-styles


    【解决方案1】:

    所以,如果您像我一样使用 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 教程提供的第一步感到困惑的人。

    【讨论】:

    • 感谢您的帮助!
    猜你喜欢
    • 2017-06-02
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    • 2013-08-04
    • 2021-03-11
    • 1970-01-01
    相关资源
    最近更新 更多