2015-02-06 66 views
1

我发现这个代码,同时搜索具有良好的图形按钮,Android Studio中的XML需要Layout_Height和Layout_Width

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > 
<corners 
android:radius="100dp" 
/> 
<gradient 
android:angle="45" 
android:centerX="35%" 
android:centerColor="#7995A8" 
android:startColor="#E8E8E8" 
android:endColor="#000000" 
android:type="linear" 
/> 
<padding 
android:left="0dp" 
android:top="0dp" 
android:right="0dp" 
android:bottom="0dp" 
/> 
<size 
android:width="270dp" 
android:height="60dp" 
/> 
<stroke 
android:width="3dp" 
android:color="#878787" 
/> 
</shape> 

然而,当我将它复制粘贴到和XML在Android Studio中的文件,它显示和错误,说,“元素形状没有必需的属性android:layout_height”,另一个错误为“元素形状没有必需的属性android:layout_width”

我试着设置android:layout_width =“”和android:layout_heigth“” 和android:layout_width“wrap_content”,android:layout_heigth =“wrap_content”,err或消失了,但我不能设置任何按钮,因为它的背景...所以我不能使用这种XML这种方式。

我应该怎样设置它们?

回答

2

<shape>没有android:layout_widthandroid:layout_height属性,因为<shape>没有放在布局资源中,您尝试放置它。 A <shape>进入可绘制资源,例如您的项目中的res/drawable/

This sample project演示使用<shape>可绘制资源。您将看到<shape>元素位于res/drawable/目录中。

您还可以在the documentation中阅读更多关于<shape>可绘制资源。

+0

哎呀,你是对的,不知道我怎么错过了!谢谢 。 – jack 2015-02-07 23:13:27

+1

如果您对此页面感到困惑,请参考CommonsWare使用Android Studio描述的内容,点击左侧文件层次结构的下拉菜单(可能是Android或Project),然后选择Project。在这个视图中,您可能会看到xml文件在布局文件夹中给您带来麻烦,而不是绘图文件夹。将文件拖放到可绘制中,错误将消失。希望有所帮助 – ColossalChris 2015-08-20 22:52:41

相关问题