【问题标题】:Primary color (sometimes) goes transparent原色(有时)变得透明
【发布时间】:2015-01-28 09:19:24
【问题描述】:

我正在使用最新的 SDK 版本 (API 21) 和支持库 21.0.2 进行开发,但在尝试实施新的 Material Design 指南时遇到了麻烦。

Material Design 说我需要我的primary color 和我的accent color 并将它们应用到我的应用程序上。但有时当我打开应用程序时,primary color 在某些小部件中变得透明,它会恢复正常,直到我关闭应用程序(使用后退按钮)并再次启动它。

这是primary color 在我的工具栏中透明的示例。

我使用 Teal 500 作为我的原色,如您所见,它仅在 android.support.v7.widget.Toolbar 中是透明的。这也发生在我的Navigation Drawer 和(有时,有时不是)另一个随机小部件中。

这是我的工具栏

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ThemeOverlay.AppCompat.ActionBar" >

我已尝试使用@color/primary?attr/colorPrimary,但没有成功。

这是我的Theme(我不知道是否相关,但以防万一):

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>     
    <item name="colorAccent">@color/accent</item>
    <item name="colorAccent">@color/accent</item>
    <item name="android:textColorPrimary">#fff</item>

    <item name="windowActionModeOverlay">true</item>
    <item name="android:textViewStyle">@style/Widget.TextView.White</item>
</style>

primary color 会发生这种情况,accent color 工作正常。我的设备运行的是 4.2.2,我还没有检查更多设备。

工具栏活动

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/my_awesome_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:ignore="MergeRootFrame" />
    </LinearLayout>

    <!-- The navigation drawer -->

    <LinearLayout
        android:id="@+id/navdrawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:orientation="vertical" >

        <!-- IN THIS IT ALSO HAPPENS -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingBottom="16dp"
            android:orientation="vertical"
            android:background="@color/primary" >

            <!-- Some stuff -->

        </LinearLayout>

        <ListView
            android:id="@+id/left_drawer"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:background="@color/black_light"
            android:divider="@color/grey"
            android:choiceMode="singleChoice" />
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

【问题讨论】:

  • 能否将您的活动的 xml 添加到您添加此工具栏的位置?
  • @NguyễnHoàiNam 完成,请查看我的编辑。提前致谢,我认为这与活动本身无关,因为它也发生在其他活动中。
  • 好吧,既然你的主题是正确的(NoActionBar 和 colorPrimary 属性等),我想确保你没有定义你的 values-v11 样式和 values-v14 样式。它可能会覆盖您最近在 4.2.2 设备上的主题。并假设您没有对工具栏进行任何编程修改(在代码中),我将尝试在我的设备上测试您的代码。
  • @NguyễnHoàiNam 谢谢,不,这个主题在valuesvalues-21 是空的,目前我没有更多的values-vX 文件夹。我正在关注official Navigation Drawer guide,并且我没有以其他方法触摸工具栏。只是说工具栏是一个示例,但这发生在更多随机小部件(即 TextView)中。
  • 这很奇怪(好吧,正如预期的那样)。考虑参考这个问题? stackoverflow.com/questions/2118251/… 。如果没有帮助,我想知道您的清单(您在哪里使用主题,在 Application 标签或 Activity 标签中),出现奇怪行为的活动类,以及您的 Widget.TextView.White 定义。但首先要考虑使用上下文的正确方法。

标签: android android-theme android-5.0-lollipop android-appcompat material-design


【解决方案1】:

问题与工具栏背景的处理方式有关。它在工具栏的所有实例之间共享,因此如果您倾向于更改工具栏的背景 alpha,那么您也会更改在其他屏幕上重复使用的可绘制对象的 alpha。

确保您没有使用工具栏的背景属性进行操作,而是在每次您想要自定义它时设置新的背景颜色/drawable。

【讨论】:

  • 哇!我不知道那个工具栏的功能!我目前无法访问该应用程序,但实际上我正在另一个 Activity 中操纵工具栏的 alpha,所以这可能是正确的答案(也许有人可以确认)。知道您是如何发现这一点的会很有趣。
  • 基于documentation:默认情况下,从同一资源加载的所有drawables实例共享一个公共状态;如果您修改一个实例的状态,所有其他实例将收到相同的修改。
  • 拯救我的一天,坦克兄弟
【解决方案2】:

也许这些支持库错误与您的问题有关。

https://code.google.com/p/android/issues/detail?id=78289
https://code.google.com/p/android/issues/detail?id=78346

遗憾的是,21.0.2 中仍未修复

【讨论】:

  • 我认为这是一个错误,第一张票看起来和我的情况几乎一样,我找到了kind of another one
  • 是的,它们看起来都是相关的。我希望尽快修复它们,因为我已经等了一个月了......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
  • 1970-01-01
  • 1970-01-01
  • 2011-11-16
  • 1970-01-01
  • 2013-01-30
  • 1970-01-01
相关资源
最近更新 更多