Android Studio中创建colors.xml后如何正确访问颜色属性?
Android颜色资源引用报错解决方案
问题背景
我在Android Studio的res目录下新建了名为color的文件夹:
随后在该color文件夹内创建了colors.xml文件,内容如下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"> <resources> <color android:name="default_screen_bg" tools:ignore="ExtraText">#f9f9f9</color> <color android:name="rounded_container_bg">#222222</color> <color android:name="rounded_container_border">#3b3f44</color> <color android:name="light_gray_header_color">#646663</color> </resources> </selector>
接着尝试在res/drawable目录下的另一个xml文件中访问colors.xml里的颜色属性,代码如下:
<?xml version="1.0" encoding="UTF-8"?> <inset android:insetLeft="1.0px" android:insetRight="1.0px" android:insetTop="0.0px" android:insetBottom="1.0px" xmlns:android="http://schemas.android.com/apk/res/android"> <selector> <item android:state_pressed="true"> <shape> <gradient android:startColor="@color/rounded_container_bg" android:endColor="@color/rounded_container_bg" android:angle="270.0" /> <corners android:radius="11.0dip" /> </shape> </item> <item> <shape> <stroke android:width="1.0px" android:color="@color/rounded_container_border" /> <gradient android:startColor="@color/colors/rounded_container_bg" android:endColor="@color/rounded_container_bg" android:angle="270.0" /> <corners android:radius="10.0dip" /> </shape> </item> </selector> </inset>
但代码中@color/colors相关行出现红色报错:
附MainActivity的Kotlin代码:
package com.example.flashlight import android.content.Context import android.hardware.camera2.CameraAccessException import android.hardware.camera2.CameraManager import android.os.Bundle import android.os.Handler import android.os.Looper import android.widget.SeekBar import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { var flashLightStatus: Boolean = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) startFlashLight() } private fun openFlashLight() { val cameraManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager val cameraId = cameraManager.cameraIdList[0] if (!flashLightStatus) { try { cameraManager.setTorchMode(cameraId, true) flashLightStatus = true } catch (e: CameraAccessException) { } } else { try { cameraManager.setTorchMode(cameraId, false) flashLightStatus = false } catch (e: CameraAccessException) { } } } private fun closeFlashLight() { val cameraManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager val cameraId = cameraManager.cameraIdList[0] if (flashLightStatus) { try { cameraManager.setTorchMode(cameraId, false) flashLightStatus = false } catch (e: CameraAccessException) { } } else { try { cameraManager.setTorchMode(cameraId, true) flashLightStatus = true } catch (e: CameraAccessException) { } } } fun startFlashLight() { var flashLightOn = false openFlashLight() flashLightOn = true val handler = Handler(Looper.getMainLooper()) handler.postDelayed(object: Runnable { override fun run() { if (flashLightOn) { closeFlashLight() } else { openFlashLight() } // 切换状态并5秒后再次执行 flashLightOn = !flashLightOn handler.postDelayed(this, 5000) } }, 5000) } }
解决步骤
1. 修正colors.xml的根标签错误
颜色资源文件的根标签必须是<resources>,当前用<selector>作为根标签会导致系统无法识别颜色资源。修改后的colors.xml:
<?xml version="1.0" encoding="utf-8"?> <resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"> <color android:name="default_screen_bg" tools:ignore="ExtraText">#f9f9f9</color> <color android:name="rounded_container_bg">#222222</color> <color android:name="rounded_container_border">#3b3f44</color> <color android:name="light_gray_header_color">#646663</color> </resources>
2. 修正颜色引用路径错误
在drawable的xml文件中,无需在颜色引用里添加文件夹名colors,直接使用颜色资源名称即可。把错误的@color/colors/rounded_container_bg改成@color/rounded_container_bg,修改后的对应代码行:
<gradient android:startColor="@color/rounded_container_bg" android:endColor="@color/rounded_container_bg" android:angle="270.0" />
3. 清理并重建项目
修改完成后,点击Android Studio顶部菜单栏的Build -> Clean Project,完成后再点击Build -> Rebuild Project,即可消除报错。
内容的提问来源于stack exchange,提问作者Daniel Lip
相关产品推荐
相关产品推荐

