2017-02-23 61 views
0

我遇到以下問題:我試圖實現類似instagram的應用程序設計,其中大部分應用程序都使用輕量級主題。但是,我還沒有找到一種爲工具欄文本和圖標元素(如導航抽屜和後退按鈕黑色)着色的方法。帶有styles.xml的Android工具欄的顏色特定項目

當採用下面style.xml

<resources> 
<!-- Base application theme. --> 
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimary">#FAFAFA</item> 
    <item name="colorPrimaryDark">#BDBDBD</item> 
    <item name="colorAccent">#FF4081</item> 
</style> 

<style name="AppTheme" parent="AppTheme.Base"></style> 

<style name="ToolbarTheme" parent="ThemeOverlay.AppCompat.ActionBar"> 
    <!-- Used to for the title of the Toolbar --> 
    <item name="android:textColorPrimary">@color/black</item> 
    <!-- Used to for the title of the Toolbar when parent is Theme.AppCompat.Light --> 
    <item name="android:textColorPrimaryInverse">@color/black</item> 
    <!-- Used to color the text of the action menu icons --> 
    <item name="android:textColorSecondary">@color/black</item> 
    <!-- Used to color the overflow menu icon --> 
    <item name="actionMenuTextColor">@color/black</item> 
</style> 
</resources> 

與這個屬性的工具欄:

android:theme="@style/ToolbarTheme" 

它產生了如下設計:

design with custom toolbar theme

,你可以看,導航抽屜是白色的,標籤標題是也是白色的。

我也嘗試過這種風格這會工作得很好,但只有當你不明確設置工具欄在你的活動是這樣的:

活動代碼:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 

Style.xml:

<!-- Base application theme. --> 
<style name="AppTheme.Base" parent="Theme.AppCompat.Light"> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 

當沒有明確的工具欄使用這種風格時,設計看起來像我想要的,但是,我不能用這種方式調用工具欄。如果您使用Theme.AppCompat.Light設置工具欄,它將引發錯誤。

還要注意兩種風格之間的區別:Theme.AppCompat.Light VS Theme.AppCompat.Light.NoActionBar

如何做到這一點任何想法?

回答

0

我剛剛與標準Android的主題就現在這個樣子:

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/container" 
    android:theme="@style/ThemeOverlay.AppCompat.Light"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:layout_scrollFlags="enterAlways" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     app:titleTextColor="@color/black"/> 
</android.support.design.widget.AppBarLayout> 

這遠比試圖建立自己的風格更容易。

相關問題