自定义具有不同背景颜色的操作栏 - Android

时间:2015-10-19 11:39:53

标签: android android-layout android-fragments

我正在尝试设置我应用的Actionbar。由于我需要自定义更多,我已经用ToolBar替换了实际的ActionBar。我想设计ActionBar,如下面的 image1 所示。

enter image description here

以下是我用来定制的工具栏代码。

custom_actionbar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/actionbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</RelativeLayout

activity_toolbar.java

 // Custom actionbar starts here
    Toolbar action = (Toolbar) findViewById(R.id.actionbar);    
    action.inflateMenu(R.menu.menu_scrollable_tabs);    
    action.setNavigationIcon(R.drawable.ct_drawer);

我正在获取下面的操作栏 image2

enter image description here

1)我面临的问题是我没有得到客户要求的导航图标和菜单图标。

2)如何使用工具栏实现此目的。如果有任何其他替代方法可以在android中提供。

请帮忙。

2 个答案:

答案 0 :(得分:1)

在styles.xml

中为样式设置主题的colorPrimary值
<item name="colorPrimary">@color/colorPrimary</item>

然后在colors.xml文件中定义您的颜色,如下所示

<color name="colorPrimary">#3F51B5</color>

请参阅设置原色的示例 http://www.dilan.me/articles/tech-notes/android-tips-2-how-to-change-the-main-colors-of-your-app/

答案 1 :(得分:0)

您可以覆盖/res/values/colors.xmal中的颜色 并更改colorPrimary的值

<color name="colorPrimary">#3F51B5</color>

或 使用此代码

  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  toolbar.setBackgroundColor(Color.BLACK);

<android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                     android:background="@android:color/black"
                    android:background="?attr/colorPrimary"
                    android:elevation="4dp"
                    />