我在搜索具有良好图形的按钮时找到了此代码,
<?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>
但是,当我将其复制并粘贴到Android Studio中的XML文件中时,它显示并显示错误消息:
“元素形状没有必需的属性android:layout_height”
和另一个错误:
“元素形状没有必需的属性android:layout_width”
我尝试设置android:layout_width=""
和android:layout_heigth""
和android:layout_width"wrap_content"
,android:layout_heigth="wrap_content"
,错误消失了,但是我不能将任何按钮设置为背景,所以我不能以这种方式使用此XML。
我应该将它们设置为什么?
android:layout_width
没有android:layout_height
和<shape>
属性,因为<shape>
不在要放置它的布局资源中。 <shape>
进入可绘制资源,例如在项目的res/drawable/
中。
This sample project演示<shape>
可绘制资源的使用。您将看到<shape>
元素位于res/drawable/
目录中。
您还可以在<shape>
中阅读有关the documentation可绘制资源的更多信息。