首页 > 解决方案 > Wear OS 无法识别自定义颜色

问题描述

我正在android studio中开发一个磨损应用程序。我定义了我的颜色

res->color->colors.xml ( 这是我的文件夹结构)

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

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

颜色.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>

活动登录.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],原始消息=,工具名称=Optional.of(AAPT)}

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

编辑

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

标签: androidandroid-layoutwear-osandroid-resourcesandroid-color

解决方案


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

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

推荐阅读