Android-Custom View要严格保持在布局的自定义边界内
创始人
2024-08-19 17:00:37
0

要实现Android-Custom View严格保持在布局的自定义边界内,可以使用以下解决方法:

  1. 在Custom View的构造函数中,使用setWillNotDraw(false)来确保Custom View会被绘制。
public class CustomView extends View {
    public CustomView(Context context) {
        super(context);
        setWillNotDraw(false);
    }
}
  1. 在Custom View的onMeasure()方法中,根据布局的宽度和高度来确定Custom View的实际大小,并确保它不超出布局的边界。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int desiredWidth = ...; // 自定义宽度
    int desiredHeight = ...; // 自定义高度

    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    int width, height;

    if (widthMode == MeasureSpec.EXACTLY) {
        width = widthSize;
    } else if (widthMode == MeasureSpec.AT_MOST) {
        width = Math.min(desiredWidth, widthSize);
    } else {
        width = desiredWidth;
    }

    if (heightMode == MeasureSpec.EXACTLY) {
        height = heightSize;
    } else if (heightMode == MeasureSpec.AT_MOST) {
        height = Math.min(desiredHeight, heightSize);
    } else {
        height = desiredHeight;
    }

    setMeasuredDimension(width, height);
}
  1. 在Custom View的onLayout()方法中,根据布局的边界,计算Custom View的实际位置。
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    int width = right - left;
    int height = bottom - top;

    // 计算Custom View的实际位置
    int customViewLeft = ...;
    int customViewTop = ...;
    int customViewRight = customViewLeft + width;
    int customViewBottom = customViewTop + height;

    layout(customViewLeft, customViewTop, customViewRight, customViewBottom);
}
  1. 在Custom View的onDraw()方法中,绘制Custom View的内容。
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // 在Custom View内部绘制内容
    // ...
}

通过以上步骤,Custom View就能够严格保持在布局的自定义边界内了。

相关内容

热门资讯

据了解!聚财平台辅助(辅助挂)... 据了解!聚财平台辅助(辅助挂)原来有挂(有挂绝活)-哔哩哔哩;无需打开直接搜索加(薇:1367043...
第四分钟外挂!大当家辅助脚本设... 第四分钟外挂!大当家辅助脚本设置,黑桃a3哟西破解版,爆料教程(存在有挂)-哔哩哔哩1、首先打开大当...
开挂透视!wepoker透视教... 开挂透视!wepoker透视教程,wepoker有没有挂,(玩家必看外挂辅助神器)是一款可以让一直输...
此事迅速冲上热搜!新道游开挂(... 此事迅速冲上热搜!新道游开挂(辅助挂)果然确实有挂(有挂诀窍)-哔哩哔哩;亲,新道游开挂这款游戏原来...
第一分钟外挂!广东雀神辅助器,... 第一分钟外挂!广东雀神辅助器,新蜜瓜大厅控制器,切实教程(有挂分享)-哔哩哔哩1、新蜜瓜大厅控制器系...
开挂透视!wepoker德州一... 无需打开直接搜索;操作使用教程:开挂透视!wepoker德州一键透视购买,wepoker透视辅助挂的...
针对!新二号辅助(辅助挂)一贯... 针对!新二号辅助(辅助挂)一贯有挂(有挂步骤)-哔哩哔哩1、下载安装好新二号辅助,进入游戏主界面,点...
第二分钟外挂!新天道辅助脚本,... 第二分钟外挂!新天道辅助脚本,丽水都莱脚本辅助视频,新2025版(有挂辅助)-哔哩哔哩1、上手简单,...
开挂透视!wpk俱乐部有挂吗,... >>>您好:,软件加微信【添加136704302】确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
受玩家影响!欢乐茶馆辅助(辅助... 受玩家影响!欢乐茶馆辅助(辅助挂)一直是有挂(有挂方案)-哔哩哔哩>>您好:软件加136704302...