2015-10-19 90 views
1

我想风格我的应用程序的动作栏。由于我需要定制更多,我用ToolBar替换了实际的ActionBar。我想要设计ActionBar,如下图所示image1自定义与不同背景颜色的动作栏 - Android

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

我正在操作栏像下面图像2

enter image description here

1)这个问题我我是cing是我没有得到像客户要求的导航图标和菜单图标。

2)如何使用工具栏来实现这一点。请提供是否有任何其他替代方式来实现这一点在Android中。

请帮忙。

回答

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/

+0

好,如何有导航图标和菜单图标类似于像我的第一个图像(即此搜索)。我想要导航图标的背景颜色,菜单图标应该填充工具栏的顶部和底部区域。这个怎么做。 – user2681579

+0

您需要创建这些图标并将它们添加为菜单项,如果您正在使用导航抽屉,则可以将其图标用于最左侧的图标 –

0

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

<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" 
        /> 
+0

如何让导航图标和菜单图标看起来类似于我的第一张图像(即image1 )。我想要导航图标的背景颜色,菜单图标应该填充工具栏的顶部和底部区域。这个怎么做。 – user2681579

+1

您可以使用具有背景的图标并将其设置为像这样导航 //toolbar.setNavigationIcon(R.drawable.ic_good); –

+0

我已经这样做了,但导航图标并未填充工具栏的顶部和底部,请参阅image2问题,我需要它像image1。请帮帮我。 – user2681579