Android 元素必须被声明为标签形状的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20790708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:28:41  来源:igfitidea点击:

Element must be declared error for tag shape

androidgradle

提问by Simon Mokhele

I'm using Android Studio I/O(Preview) 0.3.2

我正在使用 Android Studio I/O(Preview) 0.3.2

I'm using this exampleto define background gradient in my app. I get Element must be declared error. enter image description here

我正在使用此示例在我的应用程序中定义背景渐变。我得到元素必须被声明错误。在此处输入图片说明

I've checked and I'm not find any solutions. Can anyone help me how can I declare that tag in the xml

我已经检查过,但没有找到任何解决方案。任何人都可以帮助我如何在 xml 中声明该标签

回答by Simon Mokhele

This problem was caused by my xml file. My gradient XML file was in the valuesfolder, I solved this problem by moving gradient file to drawablefolder

这个问题是由我的 xml 文件引起的。我的渐变 XML 文件在values文件夹中,我通过将渐变文件移动到drawable文件夹解决了这个问题

回答by RobotCharlie

You could just right click

你可以右键单击

res

资源

and hit

并击中

New

新的

, to create a

, 创建一个

Android Resource File

安卓资源文件

, and choose

,并选择

Resource Type

资源类型

to be

成为

Drawable

可绘制

, and change whatever the default

,并更改任何默认值

root element

根元素

to

shape

形状

Yeah it's kinda buggy in Android Studio to create a new XML file other than layout and values lol.

是的,在 Android Studio 中创建一个新的 XML 文件而不是布局和值是有问题的,哈哈。

hope it can help you!!

希望能帮到你!!

回答by Abakasha Panda

I had the same problem with Android Studio , I created a directory "anim" under "res" then copied the xml file to that one then the error gone.

我在 Android Studio 上遇到了同样的问题,我在“res”下创建了一个目录“anim”,然后将 xml 文件复制到那个目录,然后错误消失了。

回答by DiRiNoiD

You need to use selector inside <set/>tags like that;

您需要在这样的<set/>标签内使用选择器;

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_enabled="true" android:state_pressed="true">
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="translationZ"
                android:valueTo="15dp"
                android:valueType="floatType" />
        </item>
        <item>
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="translationZ"
                android:valueTo="5dp"
                android:valueType="floatType" />
        </item>
    </selector>
</set>

回答by Jon

Had a similar problem with the selector tag used to create a state list animator.

用于创建状态列表动画器的选择器标签也有类似的问题。

My animation was in the anim resource folder. Had to move it to the animator folder.

我的动画在动画资源文件夹中。不得不将它移动到动画文件夹。

回答by jcw

Looking at that example, I think if you add the xmlns:androidattribute (inside the shape tag, as in the example that you are following), it will work:

看看那个例子,我想如果你添加xmlns:android属性(在 shape 标签内,就像你在下面的例子中一样),它会起作用:

xmlns:android="http://schemas.android.com/apk/res/android"

I suspect that shapeis declared there.

我怀疑那shape是在那里宣布的。