2015-10-19 97 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