2015-09-29 126 views
0

我正在使用我的应用程序的每个Activity中的工具栏类。 我有两个版本的工具栏布局,但它们仅在用RelativeLayout封装的TextView和EditText中有所不同。 这里是toolbar.xml代码:工具栏项目颜色

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/toolbar" 
android:layout_width="match_parent" 
android:layout_height="?android:attr/actionBarSize" 
android:theme="@style/AppTheme.Toolbar" 
android:popupTheme="@style/AppTheme.Toolbar.LightPopup"/> 

我跟很多android:themeandroid:popupTheme属性,但往往得到相同的结果(如下图所示),从来没有我需要什么。

在AndroidManifest.xml中所有活动都使用android:theme="@style/AppTheme"

最后,我v21\styles.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimary">@color/primary</item> 
    <item name="colorPrimaryDark">@color/primary_dark</item> 
    <item name="colorAccent">@color/accent</item> 
</style> 

<style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar"> 
    <item name="android:background">?android:attr/colorPrimary</item> 
</style> 

<style name="AppTheme.Toolbar.LightPopup"parent="ThemeOverlay.AppCompat.Light"> 
    <item name="android:background">@color/material_grey_200</item> 
</style> 

<style name="AppTheme.TextAppearance.ExtraLarge" parent="TextAppearance.AppCompat.Large"> 
    <item name="android:textSize">30sp</item> 
</style> 

这里是我得到的大部分时间: Toolbar menu Toolbar item hint 注意蓝色背景的文字。


相反,它的外观中的YouTube应用程式(以及许多其他应用,不仅从谷歌) Youtube toolbar menu Youtube toolbar item hint


我希望至少做出提示一致的背景(最好是灰色的)。 如果可能,我还想控制弹出工具栏菜单的颜色。 我尝试了几种解决方案(尤其是玩风格)和我自己的方法,但没有任何帮助。 如果您有任何想法如何解决它,请与我分享。 在此先感谢。

UPD:由于除我以外没有人回答这个问题,我最接受我的答案,因为它确实帮助了我。 Hovewer,如果你有一些补充和澄清,随时分享。例如,我仍然不知道为什么改变android:themestyle修复了一切。

回答

0

我终于想出了自己的解决方案。 原来,需要两个步骤来修复我的工具栏布局。 1)在工具栏的XML布局:

  • 删除最后一个属性
  • 通过style

这导致更换android:theme

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?android:attr/actionBarSize" 
    style="@style/AppTheme.Toolbar"/> 

2)在styles.xml文件中添加下一个项目到工具栏风格:

  • <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>(这将设置弹出就像在上面的YouTube应用程式)
  • <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>(这将设置标题和其他工具栏元素白色)

同时删除工具栏弹出框的形式。 这导致:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
     <item name="colorPrimary">@color/primary</item> 
     <item name="colorPrimaryDark">@color/primary_dark</item> 
     <item name="colorAccent">@color/accent</item> 
    </style> 

    <style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar"> 
     <item name="android:background">?android:attr/colorPrimary</item> 
     <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item> 
     <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item> 
    </style> 

    <style name="AppTheme.TextAppearance.ExtraLarge" parent="TextAppearance.AppCompat.Large"> 
     <item name="android:textSize">30sp</item> 
    </style> 
</resources> 

我希望这会帮助别人,因为它帮助了我。

相关问题