[英]Dark Toolbar with Light App Theme
I know this question has been asked before, and it worked for me before but after updating to App Compat revision 23, the Toolbar is now having a black text color (I want it white) and I didn't change a thing. 我知道之前曾有人问过这个问题,但之前对我有用。但是在更新到App Compat 23版之后,工具栏现在具有黑色文本颜色(我希望它是白色),但我没有做任何改动。
Toolbar.xml: Toolbar.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
>
</android.support.v7.widget.Toolbar>
styles.xml styles.xml
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="@attr/spinner_style">@style/spinner_style</item>
</style>
Setting toolbar to activity: 将工具栏设置为活动:
Toolbar Toolbar = (Toolbar) AppCompatActivity.findViewById(R.id.toolbar);
AppCompatActivity.setSupportActionBar(Toolbar);
ActionBar actionBar = ((AppCompatActivity)activity).getSupportActionBar();
actionBar.setTitle(title);
Change 更改
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
to 至
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
The new support libraries (AppCompat) that Android Studio brings in default project templates do not insert an important attribute to style Toolbars in older Android versions. Android Studio引入默认项目模板的新支持库(AppCompat)不会在旧版Android中为样式工具栏插入重要属性。
This is what worked for me (I'm using an AppBarLayout with Toolbar and TabLayout, as shown): 这对我有用(我将AppBarLayout与Toolbar和TabLayout一起使用,如图所示):
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/AppTheme.AppBarOverlay" <!-- Needed attribute -->
app:popupTheme="@style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:theme="@style/AppTheme.AppBarOverlay" <!-- Needed attribute -->
app:tabGravity="fill"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
I didn't research to know exactly what happened here, but older Android versions really need the theme in every element to be styled. 我没有研究确切地知道这里发生了什么,但是较旧的Android版本确实需要在每个样式中设置主题。
Hope it helps someone who is trying to find a solution to the black text in the light toolbar theme! 希望它对试图在浅色工具栏主题中为黑色文本找到解决方案的人有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.