首页 > 解决方案 > 我是 Android 开发的新手,当我想为我的 xml 文件添加颜色时,比如@color/white,我被告知“无法解析符号 @color/white”

问题描述

这是我的代码示例;

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:background="@color/white"
>

可以看到错误是红色的

标签: androidkotlin

解决方案


您需要导航到res -> values -> colors.xml file check pic here并添加以下代码:

<color name="white">#ffffff</color>

或者您可以直接在线性布局中使用此代码

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:background="#ffffff"<!--Change color according to need this is for white-->
>

推荐阅读