Custom controls in Android-adjust the position of the subviews in the custom control

In a custom view, three functions can be rewritten for drawing the view, namely onLayout, onDraw and dispatchDraw.

The execution order of these 3 functions is onLayout → onDraw → dispatchDraw.

The onLayout method is used to locate the position of the subview in this layout view.

The input parameters of this method indicate that the layout is in the upper, lower, left, and right positions of the superior view.

The implementation method is as follows

public class OffsetLayout extends RelativeLayout {

    private int mOffsetHorizontal = 0;//水平方向的偏移量
    private int mOffsetVertical = 0;//垂直方向的偏移量

    public OffsetLayout(Context context) {
        super(context);
    }

    public OffsetLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public OffsetLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            if (child.getVisibility()!=GONE){
                //计算子视图的左偏移量
                int new_left = (r-l)/2 - child.getMeasuredWidth()/2 + mOffsetHorizontal;
                //计算子视图的上方偏移量
                int new_top = (b-t)/2 - child.getMeasuredHeight()/2 + mOffsetVertical;
                //根据最新的上下左右四周边界,重新放置该子视图
                child.layout(new_left,new_top,new_left+child.getMeasuredWidth(),new_top+child.getMeasuredHeight());
            }
        }
    }

    /**
     * 设置水平方向上的偏移量
     * @param offset
     */
    public void setOffsetHorizontal(int offset){
        mOffsetHorizontal = offset;
        mOffsetVertical = 0;
        //请求重新布局,此时会出发onLayout方法
        requestLayout();
    }

    /**
     * 设置垂直方向上的偏移量
     * @param offset
     */
    public void setOffsetVertical(int offset){
        mOffsetHorizontal = 0;
        mOffsetVertical = offset;
        //请求重新布局,此时会触发onLayout方法
        requestLayout();
    }
}

When we request the requestLayout method, the onLayout method will be called again.

The layout method of View can define the position of the View in the parent layout.

Guess you like

Origin blog.csdn.net/weixin_38322371/article/details/113684768