改变 AppBar 的颜色

Change Color of AppBar

我想弄明白,ToolbarAppbarActionBar 之间有什么区别。我正在尝试将 AppBar 的颜色或其他任何颜色更改为红色。

有人提到尝试更改主题。我试过了,但不知道有什么区别,我也是 android 编程的新手。所以,如果你能帮助我。这是 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.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"
    >

    <item name="android:titleTextStyle">@style/MyTextTitle</item>
</style>

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" >
    <item name="android:titleTextStyle">@style/MyTextTitle</item>

</style>

<style name="MyTextTitle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">@color/red</item>
</style>

我希望你明白我想说的。请原谅我的无知,因为我很想学习它,所以等不及了。

在您的应用主题中试试这个:

<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>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
</style>

希望他能帮到你

试试这个,<color name="colorPrimary">#0b0b0c</color> <color name="colorPrimaryDark">#0b0b0c</color> <color name="colorAccent">#FF4081</color> Whatever color to change in hexadecimal code.then see ur app changes made it.

引用 developer.android.com

The app bar, also known as the action bar, is one of the most important design elements in your app's activities....

来自material.io

The app bar, formerly known as the action bar in Android, is a special kind of toolbar that’s used for branding, navigation, search, and actions

再次来自 developer.android.com

A Toolbar is a generalization of action bars for use within application layouts. While an action bar is traditionally part of an Activity's opaque window decor controlled by the framework, a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy. An application may choose to designate a Toolbar as the action bar for an Activity using the setActionBar() method.

如果不清楚你需要先弄清楚这两个词 generalization vs specialization 根据我们的主题 ToolbarActionBar.

支持 更多 重点功能集

进一步从blog.xamarin.com

You can think of the Toolbar as the Action Bar’s bigger sibling that has grown up and is free to roam around the house by itself.

让我们把上面提到的事情弄清楚

我制作了一个 navigationDrawerActivity,我可以很容易地创建它并为您做了一些更改以清除混乱。

我改变AppBarLayout的颜色 将工具栏从它的位置移除并设置在内部 content_main.xml 因为如果两者站在同一个位置你可能不明白!

看到你可以把它放在任何地方!

如果要更改 AppBarLayout 的颜色,请在其中添加 android:background="@color/your_color"

现在,如果您阅读上面的引述,您就会知道哪一个是 toolbar

这是我的 action_bar_main.xml(我从中删除了 toolbar 并将其设置在 content_main.xml 中)


现在您要求更改 toolbar

的文本颜色

如果您愿意,可以为此设置自己的设计布局,然后include

<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" >
            <include layout="@layout/toolbar_inflater" />
        </android.support.v7.widget.Toolbar>

如果您只想更改背景颜色,请更改 background

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

看到我已经夸大了布局,因为我 need.There 您可以根据自己的喜好添加您的视图。

或者如果您想使用代码更改工具栏中的文本

 CharSequence title = "Your App Name";
        SpannableString s = new SpannableString(title);
        s.setSpan(new ForegroundColorSpan(Color.RED), 0, title.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        getSupportActionBar().setTitle(s);

甚至您可以通过更改其主题的文本颜色来实现。阅读www.murrayc.com post and this and this 所以帖子


旁注:设置应用栏

如果你去 developer.android.com 它说,

Beginning with Android 3.0 (API level 11), all activities that use the default theme have an ActionBar as an app bar. However, app bar features have gradually been added to the native ActionBar over various Android releases. As a result, the native ActionBar behaves differently depending on what version of the Android system a device may be using. By contrast, the most recent features are added to the support library's version of Toolbar, and they are available on any device that can use the support library.

For this reason, you should use the support library's Toolbar class to implement your activities' app bars. Using the support library's toolbar helps ensure that your app will have consistent behavior across the widest range of devices. For example, the Toolbar widget provides a material design experience on devices running Android 2.1 (API level 7) or later, but the native action bar doesn't support material design unless the device is running Android 5.0 (API level 21) or later

在上面的描述下面有一个主要的子主题将工具栏添加到Activity,这强烈迫使您通过developer.android.com[=使用工具栏43=]

所以再见App BarToolbar

问好

您可以使用 Appbar 的 setBackgroundColor 方法更改颜色。 如果你有标签栏,你可以这样设置 OnTabSelectedListener

tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
        override fun onTabReselected(tab: TabLayout.Tab?) {
        }

        override fun onTabUnselected(tab: TabLayout.Tab?) {
        }

        override fun onTabSelected(tab: TabLayout.Tab) {
            when(tab.position){
                0->app_bar.setBackgroundColor(Color.parseColor("#FF0000"))
                1->app_bar.setBackgroundColor(Color.parseColor("#4286f4"))
                2->app_bar.setBackgroundColor(Color.parseColor("#f2ff00"))
            }
        }

    } )