颜色主题的暗色不起作用。

4
我以前在我的color.xml文件中设置了主要的深色颜色,状态栏的颜色也是与主要的深色颜色相同的!但是几个小时前,我创建了一个菜单资源文件,并为在我的工具栏中创建一个搜索按钮!当我运行代码时,我发现主要的深色颜色消失了,不起作用了。我搜索了很多内容以理解问题所在,但没有找到任何信息。请指导我如何解决这个问题。
style.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">

</style>

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

</style>

样式-v21:

<style name="AppTheme" parent="AppTheme.Base">
    <!-- Customize your theme here. -->
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:navigationBarColor">@color/colorNavBar</item>
    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowAllowEnterTransitionOverlap">true</item>
    <item name="android:windowAllowReturnTransitionOverlap">true</item>
    <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
    <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
</style>

toolbar.xml:

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:background="?attr/colorPrimary"
android:layout_gravity="start">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/toolbar_title"
    android:text="@string/app_name_persian"
    android:textSize="19sp"
    android:textStyle="bold"
    android:textColor="@android:color/white"
    android:layout_gravity="right"/>
<EditText
    android:layout_marginRight="20dp"
    android:id="@+id/searchEditText"
    android:layout_width="180dp"
    android:textSize="10sp"
    android:hint="@string/search_hint"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:visibility="invisible"/>

color.xml:

<resources>
<color name="colorPrimary">#0cac77</color>
<color name="colorPrimaryDark">#73ac95</color>
<color name="colorAccent">#20efd6</color>
<color name="colorNavBar">#73ac95</color>
<color name="colorNavDrawerBackColor">#f4f3f3</color>
<color name="colorCreationDate">#4acc82</color>
<color name="colorDeadDate">#d77177</color>
<color name="colorDutyName">#e5e8e9</color>

5个回答

1
在我的代码中,正如@TheAnonymous010所提到的那样,我只是将主要布局设置为了CoordinatorLayout(我已将其设置为RelativeLayout的父级)。
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.MyActivity">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimaryDark"
        android:orientation="vertical"
        android:padding="15dp">

        ....
        ....

    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

0

我找到答案了!

在主活动的xml 中,我在根布局的属性中使用了android:fitsSystemWindows="true"来避免用户使用键盘输入时窗口移动,但是我通过在清单文件中的活动属性中添加android:windowSoftInputMode="adjustPan"来修复它!


0
如果您想要更改工具栏的颜色,您可能需要将 android:backgroundandroid:background="?attr/colorPrimary" 更改为 android:background="?attr/colorPrimaryDark"

不,我不想改变工具栏的背景!我想像以前一样改变状态栏的颜色为深色主要颜色! - Ali Soleimani
2
请确保您的主布局是CoordinatorLayout。有时候状态栏颜色不会显示,除非您的主布局是CoordinatorLayout。 - TheAnonymous010

0

只需将android:backgroundandroid:background="?attr/colorPrimary"替换为android:background="@color/colorPrimaryDark"


0

不要覆盖你的主题。从你的工具栏中移除这个属性:

app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

抱歉,我刚才尝试了你的方法,但它并不是解决方案 :( - Ali Soleimani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接