You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在新版Android系统中正确获取屏幕圆角半径并适配View?

屏幕圆角适配问题:获取圆角半径时RoundedCorner返回null

问题背景

我的三星Galaxy S21手机屏幕带有圆角,需要为View添加内边距避免文字被圆角裁切。此前使用以下代码可以正常获取圆角半径并设置内边距:

private int getCornerAllowance() {
    // return 0 if this Android version doesn't do rounded corners
    int i = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        Display disp = getDisplay();
        // the corner radii are normally all the same
        // but we find the larger one just in case
        RoundedCorner rc = disp.getRoundedCorner(
            RoundedCorner.POSITION_BOTTOM_LEFT);
        int j = rc.getRadius();
        if (j > i) { i = j; }
        rc = disp.getRoundedCorner(
            RoundedCorner.POSITION_BOTTOM_RIGHT);
        j = rc.getRadius();
        if (j > i) { i = j; }
        rc = disp.getRoundedCorner(
            RoundedCorner.POSITION_TOP_LEFT);
        j = rc.getRadius();
        if (j > i) { i = j; }
        rc = disp.getRoundedCorner(
            RoundedCorner.POSITION_TOP_RIGHT);
        j = rc.getRadius();
        if (j > i) { i = j; }
    }
    return i;
}

调用方式:

mResults = new ListView(this);
int p = getCornerAllowance() * 3 / 10;
mResults.setPadding(p, 0, p, p);

问题出现

近期Android系统升级后,上述代码因getRoundedCorner返回null而崩溃。

尝试的修复方案

我猜测RoundedCorner对象需要在View附着到窗口后才会初始化,于是实现了View.OnAttachStateChangeListener并修改了代码:

首先实现接口:

... implements View.OnAttachStateChangeListener ...

修改后的圆角半径获取方法:

private int getCornerAllowance() {
    // return 0 if this Android version doesn't do rounded corners
    int i = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        WindowInsets insets = m_container.getRootWindowInsets();
        if (insets != null) {
            // the corner radii are normally all the same
            // but we find the largest one just in case
            RoundedCorner rc = insets.getRoundedCorner(
                    RoundedCorner.POSITION_BOTTOM_LEFT);
            if (rc != null) {
                int j = rc.getRadius();
                if (j > i) {
                    i = j;
                }
            }
            rc = insets.getRoundedCorner(
                    RoundedCorner.POSITION_BOTTOM_RIGHT);
            if (rc != null) {
                int j = rc.getRadius();
                if (j > i) {
                    i = j;
                }
            }
            rc = insets.getRoundedCorner(
                    RoundedCorner.POSITION_TOP_LEFT);
            if (rc != null) {
                int j = rc.getRadius();
                if (j > i) {
                    i = j;
                }
            }
            rc = insets.getRoundedCorner(
                    RoundedCorner.POSITION_TOP_RIGHT);
            if (rc != null) {
                int j = rc.getRadius();
                if (j > i) {
                    i = j;
                }
            }
        }
    }
    return i;
}

添加附着监听回调:

public void onViewAttachedToWindow(@NonNull View view) {
    int p = getCornerAllowance() * 3 / 10;
    mResults.setPadding(p,0,p,p);
}

但修改后所有RoundedCorner对象仍然返回null。

疑问

请问我哪里操作有误?是否有适用于当前Android版本的圆角半径获取代码?

内容的提问来源于stack exchange,提问作者Richard Parkins

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 12:47:07