簡體   English   中英

Android Theme.AppCompat.Light with Dark Toolbar(用於淺色文本)

[英]Android Theme.AppCompat.Light with Dark Toolbar (for light text)

我有一個使用Theme.AppCompat.Light的應用程序,我需要使用它,以便對話框具有淺色主題。 但這會使我工具欄中的文本變黑,而我希望它是白色的。 我已經嘗試將工具欄的特定app:themeAppCompat.Dark.Actionbar ,但沒有運氣......我搜索了一段時間,找不到特定的問題和答案。

這是我的默認AppTheme

<style name="AppTheme.Parent" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/blue</item>
    <item name="colorPrimaryDark">@color/dark_blue</item>
    <item name="colorAccent">@color/pink</item>

    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

這是我的toolbar.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="@dimen/abc_action_bar_default_height_material"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

為了更改Toolbar標題的顏色,您只需將屬性android:textColorPrimary添加到您的工具欄樣式

有關工具欄的更多屬性,請參見以下示例。

例子

<style name="MyToolbarStyle" parent="ThemeOverlay.AppCompat.ActionBar">
    <!-- Used to for the title of the Toolbar -->
    <item name="android:textColorPrimary">#fff</item>
    <!-- Used to for the title of the Toolbar when parent is Theme.AppCompat.Light -->
    <item name="android:textColorPrimaryInverse">#fff</item>
    <!-- Used to color the text of the action menu icons -->
    <item name="android:textColorSecondary">#fff</item>
    <!-- Used to color the overflow menu icon -->
    <item name="actionMenuTextColor">#fff</item>
    <!-- Color of the Toolbar -->
    <item name="android:background">#455a64</item>
</style>

結果

結果的圖片

您可以自己自定義工具欄,無需設置默認文本,使用您自己的 textview :

<android.support.v7.widget.Toolbar
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="?attr/colorPrimary"
   android:minHeight="@dimen/abc_action_bar_default_height_material">

   <!-- Below will add your text in the center of toolbar -->
   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Your Text"
        android:textColor=#ff0000
        android:textStyle="bold"/>

</android.support.v7.widget.Toolbar>

雖然自定義樣式可能有效,但 AppCompat 庫已經提供了一種簡單的方法來做到這一點。 如果您的Toolbar的背景顏色很暗/沒有提供足夠的對比度,請使用`ThemeOverlay.AppCompat.Dark 作為其父級:

<style name="MyTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Dark" />

然后相應地設置您的“工具欄”樣式:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/some_dark_color"
    android:theme="@style/MyTheme.PopupOverlay" />

使用顏色較淺的背景的Toolbar應使用ThemeOverlay.AppCompat.Light作為其父級。

2021年試試這個作品,活動android:theme="@style/AppTheme.NoActionBar"中的第一套主題,並在xml中設置主題

 <style name="AppTheme.NoActionBar">
    <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
    <item name="windowNoTitle">true</item>
</style>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM