带有styles.xml的Android工具栏的颜色特定项目
问题描述:
我遇到以下问题:我试图实现类似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"
它产生了如下设计:
,你可以看,导航抽屉是白色的,标签标题是也是白色的。
我也尝试过这种风格这会工作得很好,但只有当你不明确设置工具栏在你的活动是这样的:
活动代码:
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
如何做到这一点任何想法?
答
我刚刚与标准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>
这远比试图建立自己的风格更容易。