新建一个项目 我命名为ARectangle,在res目录下新建drawable包,在这个包下新建a_rectangle.xml,写入代码
<span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_shortAnimTime">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<!-- 填充的颜色 -->
<solid android:color="#9c2222"/>
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="20dp"/>
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle">
<solid android:color="#9c2222"/>
<corners android:radius="20dp"/>
</shape>
</item>
<item android:state_enabled="false">
<shape android:shape="rectangle">
<solid android:color="#902222"/>
<corners android:radius="20dp" />
</shape>
</item>
<item >
<shape android:shape="rectangle">
<solid android:color="#df3031"/>
<corners android:radius="20dp"/>
</shape>
</item>
</selector></span>
在layout下的activity_main.xml 写入一个按钮,把背景图片写成自定义的样式,如:
<span style="font-size:12px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/button1"
android:layout_width="110dp"
android:layout_height="34dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:background="@drawable/a_rectangle"
android:textColor="#ffffff"
android:textSize="15sp"
android:text="完成" />
</RelativeLayout></span>
如图所示的样式,发现显示的并不是半圆形样式,但是只要运行,在手机上显示的就是半圆形样式
本文介绍了如何在Android中实现半圆矩形的绘制。通过创建一个新项目并命名为ARectangle,然后在res/drawable目录下创建a_rectangle.xml文件,编写XML代码来定义形状。

417

被折叠的 条评论
为什么被折叠?



