【问题标题】:Element shape doesn't have required attribute android:layout_height元素形状没有必需的属性 android:layout_height
【发布时间】:2015-10-23 11:33:00
【问题描述】:

我正在使用一个形状元素作为视图的背景,就像SO question 一样。我正在使用 Android Studio,它告诉我...

Element shape doesn't have required attribute android:layout_height

Element shape doesn't have required attribute android:layout_width

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="@color/darkBrown"/>
<stroke android:layout_width="match_parent" android:layout_height="match_parent"/>
<corners android:radius="10dip"/>
<padding android:left="0dip" android:right="0dip" android:top="0dip" android:bottom="0dip"/>

</shape>

所以基本上我的问题是:如果形状在技术上不需要这些属性,为什么会出现这个错误?如何告诉 android 停止将其标记为错误或如何避免?

谢谢。

已解决:原因是它不在 drawable 文件夹中。

编辑:是的,我知道笔画部分不正确。

【问题讨论】:

    标签: android xml


    【解决方案1】:

    显示错误的原因是它不在drawable 文件夹中。 (当我在拼写错误后重命名drawable 文件夹时,它会自动移动到layout。)

    【讨论】:

      【解决方案2】:

      这是一个形状应该是什么样子的示例:

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
      <corners
          android:radius="14dp"
          />
      <solid
          android:color="#ADD8E6"
          />
      <padding
          android:left="0dp"
          android:top="0dp"
          android:right="0dp"
          android:bottom="0dp"
          />
      <size
          android:width="100dp"
          android:height="60dp"
          />
      <stroke
          android:width="3dp"
          android:color="#ADD8E6"
          />
      
      </shape>
      

      【讨论】:

      • 感谢您的回答!但我仍然无法摆脱红色!也许这是Android Studio的东西? !Screenie here 编辑:好吧,我的错,由于某种原因,当我重命名可绘制文件夹时,layout_bg.xml 移动到了布局!
      【解决方案3】:

      首先你的可绘制 XML 是不正确的。您不能只使用您喜欢的属性并期望它起作用,例如您的&lt;stroke&gt; 具有android:layout_widthandroid:layout_height,这对&lt;stroke&gt; 没有意义并且不受支持。虽然它也是android:width,但这是不同的width,与layout_width 没有任何共同之处。 Docs are here 并包含所有支持的属性。

      【讨论】:

      • 这与错误无关,因为即使在使用技术上正确的宽度和高度属性后我仍然得到它(复制您上面的答案提供的代码)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-04
      • 2012-01-07
      • 2017-05-03
      • 2021-12-31
      • 2019-06-26
      • 1970-01-01
      相关资源
      最近更新 更多