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就能够严格保持在布局的自定义边界内了。

相关内容

热门资讯

七分钟辅助!安装不了wepok... 七分钟辅助!安装不了wepoker,uupoker透视,要领教程(新版有挂)1、玩家可以在安装不了w...
第5分钟辅助!佛手大菠萝辅助,... 第5分钟辅助!佛手大菠萝辅助,wejoker开挂,积累教程(有挂方法)第5分钟辅助!佛手大菠萝辅助,...
第2分钟辅助!hhpoker哪... 第2分钟辅助!hhpoker哪个俱乐部靠谱,sohoopoker辅助,法门教程(确实有挂)1、任何h...
1分钟辅助!wepoker破解... 1分钟辅助!wepoker破解是真的还是假的,wepoker辅助器是真的吗,学习教程(有挂助手)所有...
三分钟辅助!wepoker网页... 三分钟辅助!wepoker网页版透视方法,hhpoker脚本,妙招教程(讲解有挂)1、完成wepok...
7分钟辅助!大菠萝免费辅助,w... 7分钟辅助!大菠萝免费辅助,wpk透视是真的假的,总结教程(有挂讲解)1、进入游戏-大厅左侧-新手福...
5分钟辅助!hhpoker作弊... 5分钟辅助!hhpoker作弊码怎么用,约局吧作弊脚本,指引教程(的确有挂)1)hhpoker作弊码...
6分钟辅助!德州透视插件,wp... 6分钟辅助!德州透视插件,wpk辅助是什么,方式教程(了解有挂)1.wpk辅助是什么 选牌创建新账号...
第二分钟辅助!拱趴大菠萝机器人... 第二分钟辅助!拱趴大菠萝机器人,aapoker怎么选牌,窍门教程(有挂攻略)1、拱趴大菠萝机器人辅助...
第一分钟辅助!hhpoker可... 第一分钟辅助!hhpoker可以控制牌吗,德普之星透视辅助软件,资料教程(有挂方式)1)德普之星透视...