更改AppBar的颜色 [英] Change Color of AppBar

查看:60
本文介绍了更改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>

希望您能理解我要说的话.并且请原谅我对我的无知,因为我渴望学习它,所以等不及了.

解决方案

引用 developer.android .com

应用栏(也称为操作栏)是最常用的应用栏之一 应用活动中的重要设计元素....

material.io

应用栏(以前在Android中称为操作栏)是一种特殊功能 用于品牌,导航,搜索和 动作

再次

来自 developer.android.com

工具栏是在内部使用的操作栏的概括 应用程序布局.传统上,操作栏是 活动的不透明窗口装饰,由框架,工具栏控制 可以放置在视图中的任意任意嵌套级别 等级制度.应用程序可以选择将工具栏指定为 使用setActionBar()方法的活动"操作栏.

如果不清楚,您需要先根据主题Toolbar支持 more 清除这两个词一般化专业化重点功能集比ActionBar.

进一步来自 blog.xamarin.com

您可以将工具栏视为操作栏的更大兄弟 已经长大,可以自由地在房子周围漫游.

让我们清除上面提到的内容

我制作了一个navigationDrawerActivity,可以轻松创建它,并进行了一些更改以帮助您清除混乱.

我改变AppBarLayout的颜色 从其位置中删除了工具栏,并将其放置在content_main.xml内部,因为如果两个工具都位于同一位置,您可能就不会明白这个想法!

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

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

现在,如果您阅读以上引用,您就会知道哪个是toolbar

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


现在,您要求更改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" />

请参见我根据需要调整了布局.您可以在其中添加视图作为首选项.

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

 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 帖子和因此发布


侧面说明:设置应用栏

如果您访问 developer.android.com ,则说,

从Android 3.0(API级别11)开始,所有使用 默认主题有一个ActionBar作为应用程序栏.但是,应用栏 功能已逐渐添加到本机ActionBar中 各种Android版本.结果,本机ActionBar的行为 不同,具体取决于设备的Android系统版本 可能正在使用.相比之下,最新功能已添加到 支持库的工具栏版本,并且在任何 可以使用支持库的设备.

因此,您应使用支持库的工具栏类来 实施活动的应用栏.使用支持库的 工具栏可帮助确保您的应用具有一致的行为 在最广泛的设备中.例如,工具栏小部件 在运行Android 2.1的设备上提供实质性的设计体验 (API级别7)或更高版本,但本机操作栏不支持 材料设计,除非设备运行的是Android 5.0(API级别) 21)或更高版本

下面的描述中有一个主要的子主题向活动中添加工具栏,通过

I hope you understand what i am trying to say. And forgive me for my ignorance as i am keen to learn it so can't just wait.

解决方案

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

and from 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

again from 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.

If this is not clear you need to clear theese two words first generalization vs specialization according to our topic Toolbar supports a more focused feature set than ActionBar.

Further from 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.

Let's clear things above mentioned

I have made a navigationDrawerActivity which i can easily create and did some change for you to clear the mess.

I change color of the AppBarLayout Removed the toolbar from it's position and set in inside content_main.xml because if both stand in the same spot you might not get the idea!

See you can place it anywhere!

If you want to change the color of AppBarLayout add this inside it android:background="@color/your_color"

Now if you read above quotes you know which one is toolbar

This is my action_bar_main.xml (i removed toolbar from this and set it inside content_main.xml)


Now you asked to change the text color of toolbar

If you want you can set your own design layout for that then include it.

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

If you only want to change the background color change it's 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" />

See i have inflated a layout as i need.There you can add your views as your preference.

or if you want to change the text in tool bar using code

 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);

or even you can do it by changing the textcolor of it's theme. Read www.murrayc.com post and this and this so posts


Side Note : Setting Up the App Bar

If you go the developer.android.com it says,

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

Below above description there is a main sub topic Add a Toolbar to an Activity which strongly force you to use tool bar by developer.android.com

So Bye bye App Bar say Hi to Toolbar

这篇关于更改AppBar的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆