【问题标题】:Custom Colors are not recognized for Wear OSWear OS 无法识别自定义颜色
【发布时间】:2018-06-10 15:48:28
【问题描述】:

我正在 android studio 中开发一个磨损应用程序。我在

中定义了我的颜色

res->color->colors.xml ()

当我为手机或平板电脑打开一个新项目时,colors.xml 被 Android Studio 放置在 values 文件夹中。但这一次,Android Studio 为 Wear 项目创建了一个名为 color 的新文件夹,并自动将 colors.xml 放入其中。

问题是我不能将 colors.xml 中定义的颜色用于磨损项目。

colors.mxl

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<color android:name="colorPrimary">#006064</color>
<color android:name="colorPrimaryDark">#004c40</color>
<color android:name="colorAccent">#82b1ff</color>
<color android:name="black_alpha_40">#66000000</color>

</selector>

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.wear.widget.BoxInsetLayout
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:background="@color/dark_grey"
android:padding="@dimen/box_inset_layout_padding"
tools:context=".activities.LoginActivity"
tools:deviceIds="wear">


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp"
        app:boxedEdges="all">
    <EditText
        android:id="@+id/loginEmailEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Email"
        android:inputType="textEmailAddress"
        android:layout_gravity="top"
        />

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="top"
            android:src="@drawable/logo"
            android:tint="@color/colorAccent" --->THERE IS AN ERROR HERE
            />
    </FrameLayout>

    <EditText
        android:id="@+id/loginPasswordEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Parola"
        android:inputType="textPassword"
        android:layout_gravity="center"/>

    <ImageButton
        android:id="@+id/loginButton"
        android:background="@drawable/circular_button"
        android:layout_height="50dp"
        android:layout_width="50dp"
        android:src="@drawable/done_white"
        android:text="Giriş"
        android:onClick="attemptLogin"
        android:layout_gravity="bottom|center"/>
</FrameLayout>
</android.support.wear.widget.BoxInsetLayout>

当我想在我的 activity_login.xml 文件中使用它们时,没有任何自定义颜色(colorPrimary、colorPrimaryDark 等)无法识别,并且 android studio 会出现以下错误。

错误:找不到资源颜色/colorAccent(又名 com.muhendis.diabetwatch:color/colorAccent)。 消息{kind=ERROR, text=error: resource color/colorAccent (aka com.muhendis.diabetwatch:color/colorAccent) not found., sources=[/Users/muhendis/Documents/AndroidStudioProjects/DiabetWatch/app/src/main/ res/layout/activity_login.xml:32], original message=, tool name=Optional.of(AAPT)}

所以我的问题是如何在开发穿戴应用程序时摆脱这个错误并定义自定义颜色?

编辑

它是colors.xml 不是colors.mxl。我拼错了。

【问题讨论】:

    标签: android android-layout wear-os android-resources android-color


    【解决方案1】:

    您有几个可见的错误。目前尚不清楚您的代码中有多少以及您的 SO 问题中有多少,但解决这些问题,我敢打赌它会起作用:

    1. 您曾将文件命名为 colors.**mxl**。拼写很重要!
    2. 它需要在res\values,而不是res\color

    【讨论】:

    • 感谢您的回答。 colors.mxl 是我的错,我问的时候拼错了。我编辑了这个问题。问题是当您尝试添加资源文件并选择颜色时(右键单击 res -> 新建 -> Android 资源文件 -> 资源类型:颜色,文件名:colors.xml)Android Studio 在颜色目录中添加 colors.xml自动恢复。 (res->color->colors.xml) 我不知道为什么 Android Studio 会这样添加,即使它在布局中不被识别。添加到 res\values 是有效的。谢谢。
    • 这是因为这不是一个颜色资源文件。它的根 XML 元素是resources,这意味着该文件可以包含多个项目,这些项目是由它们各自的 XML 元素指定的资源 。因此,它位于values 子目录中。其他资源文件包含由其路径名指定的单个项目,例如res\color\my_dynamic_color.xml
    • 现在我明白了其中的区别。 @String 你太棒了谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 2016-01-30
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多