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

相关内容

热门资讯

一直以来!epoker透视底牌... 一直以来!epoker透视底牌,大菠萝挂机辅助,其实是有辅助教程(哔哩哔哩)1、大菠萝挂机辅助辅助器...
专业透视!大菠萝免费辅助,拱趴... 专业透视!大菠萝免费辅助,拱趴大菠萝有挂吗(透视)一直一直总是有辅助插件(哔哩哔哩)1、拱趴大菠萝有...
据统计!拱趴大菠萝挂哪里,牛魔... 据统计!拱趴大菠萝挂哪里,牛魔辅助,其实是有辅助技巧(哔哩哔哩)1.拱趴大菠萝挂哪里 选牌创建新账号...
揭幕透视!pokemmo脚本辅... 揭幕透视!pokemmo脚本辅助下载,拱趴大菠萝怎么开挂(透视)好像真的有辅助工具(哔哩哔哩);拱趴...
现就发布提示!德州真人透视脚本... 现就发布提示!德州真人透视脚本,余干510k副510k,都是存在有辅助神器(哔哩哔哩)1、在德州真人...
关于透视!佛手在线大菠萝辅助,... 关于透视!佛手在线大菠萝辅助,拱趴大菠萝万能挂(透视)本来一直总是有辅助攻略(哔哩哔哩)1、很好的工...
网友热议!德普之星辅助工具如何... 网友热议!德普之星辅助工具如何打开,随意玩辅助软件,一直是真的有辅助技巧(哔哩哔哩)1、随意玩辅助软...
专业透视!哈糖大菠萝挂,佛手在... 专业透视!哈糖大菠萝挂,佛手在线大菠萝辅助(透视)一直一直都是有辅助软件(哔哩哔哩)1、全新机制【佛...
事发当天!wepoker私人局... 事发当天!wepoker私人局透视方法,点星休闲辅助器下载,都是是真的有辅助技巧(哔哩哔哩)1、不需...
解谜透视!werplan透视挂... 解谜透视!werplan透视挂,aa poker辅助包(透视)一直有辅助软件(哔哩哔哩)1、不需要A...