android的兼容性真是一个不可忽略的问题,今天测试时发现使用xml定义Shape drawable在android 2.1上存在bug
<?xml version="1.0" encoding="UTF-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#319ED0"/>
<corners
android:bottomLeftRadius="0.0dip"
android:bottomRightRadius="8.0dip"
android:topLeftRadius="8.0dip"
android:topRightRadius="0.0dip" />
</shape>
把上面的xml定义作为Button的background时,在android 2.2和android 2.3都能正常显示圆角;但在android 2.1的真机和模拟器上均无法显示圆角
研读了一下android doc,发现这么一句话
Note: Every corner must (initially) be provided a corner radius greater than 1, or else no corners are rounded. If you want specific corners to not be rounded, a work-around is to use android:radius to set a default corner radius greater than 1, but then override each and every corner with the values you really want, providing zero ("0dp") where you don't want rounded corners.
于是改为
<?xml version="1.0" encoding="UTF-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#319ED0"/>
<corners
android:radius="1dip"
android:bottomLeftRadius="0dip"
android:bottomRightRadius="8.0dip"
android:topLeftRadius="8.0dip"
android:topRightRadius="0dip" />
</shape>
一切正常
google了一下,发现http://code.google.com/p/android/issues/detail?id=939,原来多年以前就有人提过这个bug了,高人给出了建议
<corners android:bottomRightRadius="0.1dp"
android:bottomLeftRadius="7dip"
android:topLeftRadius="7dip"
android:topRightRadius="0.1dp"/>
使用很小的radius来到达一个近似与“方角”的效果,恩,很好的work around