如何更改上面工具栏的颜色?

时间:2018-02-07 12:32:07

标签: android toolbar

我是Android开发的新手,我不知道它实际上是什么叫(工具栏的上方空间如下图所示,或时间和电池标志显示的位置。)我想填充自定义颜色所以请告诉我如何填充它的颜色。 image 谢谢......

以下是我的XML文件

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eee">
<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.devbhoomimedia.maangal.ProfilesActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:titleTextColor="#ffffff"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            app:tabTextColor="#ffffff"
            app:tabMode="scrollable"
            app:tabGravity="fill"
            android:background="#2f6b00"
            app:tabIndicatorColor="@android:color/white"
            app:tabIndicatorHeight="4dp"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    <include layout="@layout/content_main" />

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

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/menu_nevigation"
    app:itemTextAppearance="?android:attr/textAppearance"/>

3 个答案:

答案 0 :(得分:1)

尝试以下:

转到应用 - &gt; res - &gt;价值 - &gt; colors.xml - 更改colorPrimaryDark颜色

<color name="colorPrimaryDark">#030239</color>

,或者

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/color_primary</item>
    <item name="colorPrimaryDark">@color/color_secondary</item>
    <item name="colorAccent">@color/color_accent</item>
     <!---Below is the code for status bar color------>
    <item name="android:statusBarColor">@color/color_secondary</item>
</style>

或,

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.RED);
}

答案 1 :(得分:0)

工具栏上方的视图在Android中被官方称为status bar并更改status bar的颜色,请查看此问题的答案How to change the status bar color in android

答案 2 :(得分:0)

您可以将此用于API Level 21及更高版本: 放置这是您的值-v21 / styles.xml
- &GT;工具栏顶部的空间称为StatusBar。

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/color_primary</item>
        <item name="colorPrimaryDark">@color/color_secondary</item>
        <item name="colorAccent">@color/color_accent</item>
        <item name="android:statusBarColor">@color/color_primary</item>
    </style>
</resources>

Here检查热门答案。

快乐编码......