交叉布局

交叉布局

package ngyb.crossover;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

/**
 * 作者:南宫燚滨
 * 描述:交叉布局
 * 邮箱:nangongyibin@gmail.com
 * 时间: 2018/4/18 14:30
 */
public class NGYBCrossLayout extends RelativeLayout {
    private static final String TAG = "CrossLayout";
    private  boolean isLeft;
    public NGYBCrossLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int top = 0;
        for (int i = 0; i < getChildCount(); i++) {
            View childAt = getChildAt(i);
            int left = 0;
            if (isLeft){
                if (i%2==0){
                    left =getMeasuredWidth()-childAt.getMeasuredWidth();
                }else{
                    left = 0;
                }
            }else{
                if (i%2==0){
                    left =0;
                }else{
                    left = getMeasuredWidth()-childAt.getMeasuredWidth();
                }
            }
            int right = left+childAt.getMeasuredWidth();
            int bottom = top+childAt.getMeasuredHeight();
            Log.e(TAG, "onLayout: "+left+"="+right+"="+top+"="+bottom );
            childAt.layout(left,top,right,bottom);
            top+=childAt.getMeasuredHeight();
        }
    }

    public void change() {
        isLeft = !isLeft;
        requestLayout();
    }
}

https://github.com/nangongyibin7219/Android_CrossLayout

转载于:https://www.cnblogs.com/nangongyibin/p/10324263.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值