【问题标题】:Set drawable as Background to view is returning drawable not found将可绘制对象设置为背景以查看返回未找到可绘制对象
【发布时间】:2018-11-30 10:45:14
【问题描述】:

i 在此之前一直将可绘制对象作为背景添加到我的视图中。但是最近当我将可绘制对象作为背景添加到任何视图(例如 LinearLayout、视图、TextView)时,我得到了这个错误

 error: resource drawable/rectangle_white (aka merchant.com.our.nextlounge:drawable/rectangle_white) not found.
Message{kind=ERROR, text=error: resource drawable/rectangle_white (aka merchant.com.our.nextlounge:drawable/rectangle_white) not found., sources=[C:\Users\USER\Documents\Exolve Project\NextLounge\app\src\main\res\layout\activity_dash_auth.xml:45], original message=, tool name=Optional.of(AAPT)}

我已刷新、清理、构建,但它无法正常工作。唯一可行的方法是添加多个可绘制 API 文件夹,例如 drawable-v21,drawable-v22,drawable-v23

让它工作。 请问如何解决这个问题。

<?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.ConstraintLayout 
  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"
tools:context=".activity.DashAuthActivity">
<include layout="@layout/background_auth"/>

  <include layout="@layout/logo"
   android:id="@+id/logo"
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   app:layout_constraintLeft_toLeftOf="parent"
   app:layout_constraintRight_toRightOf="parent"
   app:layout_constraintTop_toTopOf="parent"/>
<TextView
    android:id="@+id/text"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="@string/intro_text"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/logo"
    app:layout_constraintBottom_toBottomOf="@id/linearLine"
    android:layout_margin="20sp"
    app:layout_constraintVertical_bias="0.0"
    android:padding="8dp"
    android:textAlignment="center"
    android:textColor="@color/white"/>
<LinearLayout
    android:id="@+id/linearLine"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="4"
    android:padding="4dp"
    android:layout_margin="24dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintVertical_bias="0.96"
    app:layout_constraintBottom_toTopOf="@+id/linearButton"
    android:layout_marginBottom="20sp">
    <View
        android:layout_width="0dp"
        android:layout_height="6dp"
        android:background="@drawable/rectangle_white"
        android:layout_weight="2.5"/>
    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_weight="0.3"/>
    <View
    android:layout_width="0dp"
    android:layout_height="6dp"
    android:background="@drawable/rectangle_white_line"
    android:layout_weight="1.2"/>
</LinearLayout>
 <LinearLayout
android:id="@+id/linearButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4"
android:padding="4dp"
android:layout_margin="20dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent">

   <Button
    android:id="@+id/buttonSignIn"
    android:layout_width="0dp"
    android:layout_height="48dp"
    android:layout_weight="2"
    android:layout_marginRight="4dp"
    android:layout_marginEnd="4dp"
    android:text="@string/sign_in"
    android:textColor="@color/black"
    android:textAllCaps="false"
    android:textSize="18sp"
    android:background="@color/white"/>
    <Button
    android:id="@+id/buttonSignUp"
    android:layout_width="0dp"
    android:layout_height="48dp"
    android:layout_weight="2"
    android:layout_marginLeft="4dp"
    android:layout_marginStart="4dp"
    android:text="@string/sign_up"
    android:textAllCaps="false"
    android:textSize="18sp"
    android:textColor="@color/white"
    android:background="@color/orange"/>

下面是 rectangle_white.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
  <stroke android:color="#fff"
     android:width="2dp"/>

</shape>

【问题讨论】:

  • 发布你的代码让其他人了解你到底在做什么
  • 你确定drawable文件夹中有rectangle_white文件吗?因为在其他你使用rectangle_white_line

标签: android xml kotlin


【解决方案1】:

在运行时,将选择的drawable是最适合设备的android版本的,
除非只有1个drawable文件夹,在这种情况下只有1个drawable。
由于drawable-v21drawable-v22drawable-v23 等中的其他版本drawable 可以工作,
但是主要drawable 中的drawable 不起作用,这意味着后一个drawable 是问题所在。
我建议您检查它是否存在 xml 结构中的错误,或者是否可以重新创建它。

【讨论】:

  • 我只有一个可绘制文件夹,它在构建过程中显示此错误。但如果我创建单独的 API 版本文件夹,它会成功构建
  • 如果我创建单独的 API 版本文件夹,它会成功构建 在创建这些文件夹后,你是否会重新创建此可绘制对象并将其放入这些文件夹中?
  • 是的,我将它们复制到新的可绘制文件夹中,它工作正常。但它的缺点是我必须为每个 API 版本创建一个可绘制文件夹,以便它在不同的 android 手机上运行
  • 那么主要的drawable就是问题所在。你能重新创建它吗?
  • 我已将其删除并创建了一个新的可绘制目录并将所有 xml 文件复制回来,但仍然遇到同样的问题
【解决方案2】:

您好,感谢大家的帮助。我所要做的就是删除包含所有内容的整个可绘制文件夹,然后我创建了一个新的可绘制文件夹并重新创建了所有文件。 一旦我这样做了,我就运行它,它适用于所有 android 设备。所以我不必为不同的 API 版本创建特定的文件夹

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 2016-02-21
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    相关资源
    最近更新 更多