android2.1中圆角的bug

android的兼容性真是一个不可忽略的问题,今天测试时发现使用xml定义Shape drawable在android 2.1上存在bug

  1. <SPANstyle="FONT-SIZE: 18px"><?xmlversion="1.0"encoding="UTF-8"?>
  2. <shapeandroid:shape="rectangle"
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <solidandroid:color="#319ED0"/>
  5. <corners
  6. android:bottomLeftRadius="0.0dip"
  7. android:bottomRightRadius="8.0dip"
  8. android:topLeftRadius="8.0dip"
  9. android:topRightRadius="0.0dip"/>
  10. </shape></SPAN>

把上面的xml定义作为Button的background时,在android 2.2和android 2.3都能正常显示圆角;但在android 2.1的真机和模拟器上均无法显示圆角

研读了一下android doc,发现这么一句话

  1. 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.

于是改为

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <shapeandroid:shape="rectangle"
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <solidandroid:color="#319ED0"/>
  5. <corners
  6. android:radius="1dip"
  7. android:bottomLeftRadius="0dip"
  8. android:bottomRightRadius="8.0dip"
  9. android:topLeftRadius="8.0dip"
  10. android:topRightRadius="0dip"/>
  11. </shape>

一切正常

原来多年以前就有人提过这个bug了,高人给出了建议

  1. <cornersandroid:bottomRightRadius="0.1dp"
  2. android:bottomLeftRadius="7dip"
  3. android:topLeftRadius="7dip"
  4. android:topRightRadius="0.1dp"/>

使用很小的radius来到达一个近似与“方角”的效果,恩,很好的work around

上一篇:js五星评分


下一篇:Android Studio 代码无提示,无颜色区分