Android shape corners bottomRightRadius XML右下角圆角

1次阅读

android:bottomrightradius 单独设置无效,必须同时声明 android:topleftradius、android:toprightradius、android:bottomleftradius 和 android:bottomrightradius 四个属性,且单位须为 dp,其他三角设为 0dp 才能实现仅右下角圆角。

Android shape corners bottomRightRadius XML右下角圆角

android:bottomRightRadius 在 中根本不起作用

Android 的 shape xml 中,android:bottomRightRadius 单独设置不会生效——系统会直接忽略它。这是官方文档没写清楚、但实际行为非常确定的限制。

原因很简单:Android 要求圆角必须成对或统一控制。单独设某一个角的半径,底层 RoundRectShape 构造逻辑不支持这种“部分指定”。

  • 只有 android:radius 全局生效(四个角一致)
  • 或者用 android:topLeftRadius + android:topRightRadius + android:bottomLeftRadius + android:bottomRightRadius 四个属性**同时出现**,才能分别控制
  • 漏掉任意一个,系统就回退到默认(0 或全局 radius)

要只圆右下角,必须四角全写且其他设为 0dp

想实现「仅右下角圆角」,不是只写 android:bottomRightRadius,而是显式把其余三个角设为 0dp,哪怕它们本来就是 0。

示例(放在 res/drawable/rounded_bottom_right.xml):

<shape xmlns:android="http://schemas.android.com/apk/res/android">     <solid android:color="#FF6200" />     <corners         android:topLeftRadius="0dp"         android:topRightRadius="0dp"         android:bottomLeftRadius="0dp"         android:bottomRightRadius="8dp" /> </shape>
  • 四个 android:*Radius 必须共存,缺一不可
  • 单位必须是 dp(不能是 px 或无单位),否则在某些 Android 版本会解析失败
  • 值为 0dp 是安全的;写 0 可能被误读为 0px,导致意外圆角

API 21+ 可用 android:topLeftRadius 等但兼容性有坑

这些独立圆角属性从 API 21(Lollipop)才正式支持。低于此版本(如 19)会完全忽略,渲染为直角。

  • 如果你的 minSdk ≤ 20,不能依赖这些属性——要么降级用 android:radius,要么改用 VectorDrawable 或自定义 Drawable
  • 即使在 API 21+,部分国产 ROM(如旧版 MIUI、EMUI)对 corners 解析不严格,可能丢弃未声明的角
  • 测试时务必在真机(尤其低端 Android 5.0 设备)上验证,模拟器有时过于宽容

更稳的方式:用 layer-list + 透明遮罩

当需要精确控制单角圆角,又必须兼容低版本时,最可靠的做法不是硬刚 shape,而是用两层 drawable 拼出来。

原理:底层是完整矩形色块,上层是带“右下缺口”的白色(或父容器背景色)遮罩,露出底层右下角。

  • 遮罩层用 shape + android:radius="8dp",再用 android:gravity="bottom|end" 定位到右下
  • 这样绕过所有单角 radius 兼容问题,API 14+ 都稳定
  • 缺点是多一层绘制,但对普通按钮/卡片背景几乎无感知

右下角圆角看着简单,真正卡住人的永远是那句「为什么我写了就是不生效」——答案往往就藏在「四个 radius 必须齐活」和「0dp 不是可选而是必需」里。

text=ZqhQzanResources