Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么安卓无法解析符号,但仍然有效?_Android_Xml_Android Layout_Attributes_Cannot Find Symbol - Fatal编程技术网

Android 为什么安卓无法解析符号,但仍然有效?

Android 为什么安卓无法解析符号,但仍然有效?,android,xml,android-layout,attributes,cannot-find-symbol,Android,Xml,Android Layout,Attributes,Cannot Find Symbol,我有一个框架布局,点击会产生连锁反应。为此,我在FrameLayout上有以下标记: android:foreground="?attr/selectableItemBackgroundBorderless" 问题是,我得到一个错误,上面写着:“无法解析符号”?attr/selectableItemBackgroundBorderless'。尽管有错误,我仍然可以运行项目,框架布局具有我想要的效果 但是如果我试着用这个标签代替: android:foreground="?android:att

我有一个框架布局,点击会产生连锁反应。为此,我在FrameLayout上有以下标记:

android:foreground="?attr/selectableItemBackgroundBorderless"
问题是,我得到一个错误,上面写着:“无法解析符号”?attr/selectableItemBackgroundBorderless'。尽管有错误,我仍然可以运行项目,框架布局具有我想要的效果

但是如果我试着用这个标签代替:

android:foreground="?android:attr/selectableItemBackgroundBorderless"
我又犯了一个错误。它说这个标签需要API级别21

所以我的问题是,正确的方法是什么?我是否应该继续使用无法解析符号的符号并忽略错误?有没有其他方法可以使另一个标签具有类似的行为

对于延伸到视图之外的涟漪。它将被利用,并以, 具有非空背景的视图的最近父级

  • SelectableItemBackgroundless
    是在中引入的一个新属性 API级别21
如果要使用此功能,请打开模块级别的build.gradle,然后设置

    compileSdkVersion 27
    buildToolsVersion "27.0.3"
    defaultConfig {
        applicationId "\\"
        minSdkVersion 21
        targetSdkVersion 27
确保在
项目级
部分中添加
google()

repositories {
        ....
        google() //maven { url 'https://maven.google.com' }
    }
结构

<!-- Background drawable for borderless standalone items that need focus/pressed states. -->
 <attr name="selectableItemBackgroundBorderless" format="reference" />
欲了解更多信息

从中,您可以看到
?attr/selectableItemBackgroundBorderless
?android:attr/selectableItemBackgroundBorderless的别名:

<item name="selectableItemBackgroundBorderless">?android:attr/selectableItemBackgroundBorderless</item>
当您使用以下各项时:

android:foreground="?attr/selectableItemBackgroundBorderless"
<style name="MyStyle">
    <item name="android:foreground">?attr/selectableItemBackgroundBorderless</item>
</style>

您正在使用支持库提供的资源。在我看来,这个错误是因为androidstudio试图知道原始资源,而在api 21以下的地方找不到原始资源。由于找不到原始资源,它将退回到支持库提供的资源。

与其直接分配
android:foreground
,不如让您的
FrameLayout
使用以下样式:

<FrameLayout 
    style="@style/MyStyle"
    ....>
然后在
res/values-v21/styles.xml上添加带有
android:
前缀的符号:

<style name="MyStyle">
    <item name="android:foreground">?android:attr/selectableItemBackgroundBorderless</item>
</style>

?android:attr/SelectableItemBackgroundless无边界
android:前台=“?android:attr/selectableItemBackgroundBorderless”
仅用于api 21或更高版本。如果您想要app compat版本,您应该删除
android
部分。(您还可以删除attr零件,这相当于不删除它)

以下两种方法都适用于低于21的api,并且完全相同:

android:foreground=“?attr/selectableitemsbackgroundless”

android:前台=“?selectableItemBackgroundBorderless”
它帮助我:

  • 在Android Studio中打开您的项目
  • 构建->清理项目
  • 打开项目目录
  • 删除以下目录:.idea、.gradle
  • 回到安卓工作室
  • 文件->使缓存无效/重新启动…->失效并重新启动
  • 文件->将项目与渐变文件同步

  • 希望这对您有所帮助。

    将其设置为
    android:background=“?android:attr/selectableitemsbackground”
    使用
    File->Open…
    重新导入项目帮助我解决了一个类似的问题,即无法解决ID问题。请检查我的答案,并告诉我是否缺少什么。
    <style name="MyStyle">
        <item name="android:foreground">?attr/selectableItemBackgroundBorderless</item>
    </style>
    
    <style name="MyStyle">
        <item name="android:foreground">?android:attr/selectableItemBackgroundBorderless</item>
    </style>