【问题标题】:Android customizing Toolbar for Material Design用于 Material Design 的 Android 自定义工具栏
【发布时间】:2015-04-07 13:49:47
【问题描述】:

我正在尝试使用工具栏实现 Material 风格的 ActionBar。到目前为止,我制作了一个有效的 Material 风格的 ActionBar,但无法更改其图标和文本颜色。我已经设置了一个

Theme.AppCompat.Light.NoActionBar

在 style.xml 和

android:theme="@style/ThemeOverlay.AppCompat.Dark"

作为工具栏主题。

有什么办法可以把文字颜色和黑色图标改成白色吗?

来源如下,

工具栏:

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark" >

    </android.support.v7.widget.Toolbar>

应用主题:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

        <item name="colorPrimary">@color/ColorPrimary</item>
        <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
    </style>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.materialactionabar.MainActivity" >

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

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <fragment
            android:id="@+id/navigation_drawer"
            android:name="com.example.materialactionabar.NavigationDrawerFragment"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start" />
    </android.support.v4.widget.DrawerLayout>

【问题讨论】:

    标签: android android-layout android-styles


    【解决方案1】:

    在你的样式中添加如下:

    <item name="spinBars">true</item>
    <item name="color">@color/white</item>
    

    完整代码如下:

    <resources>
     <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/ColorPrimary</item>
        <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
        <item name="windowActionBar">false</item>
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    </style>
    
    <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@color/white</item>
    </style>
    

    【讨论】:

    • 感谢@Ranjith,让它正常工作。我有一个小问题,为什么我们需要在主题中添加另一种风格?我的意思是它应该在不添加新样式的情况下工作。
    猜你喜欢
    • 2015-03-20
    • 1970-01-01
    • 2015-02-04
    • 2014-12-24
    • 2015-03-26
    • 1970-01-01
    • 2015-07-22
    • 2020-04-29
    • 1970-01-01
    相关资源
    最近更新 更多