码迷,mamicode.com
首页 > 其他好文 > 详细

交叉布局

时间:2019-01-26 17:56:28      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:ack   getch   top   for   protected   sla   idg   idt   attr   

交叉布局

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

交叉布局

标签:ack   getch   top   for   protected   sla   idg   idt   attr   

原文地址:https://www.cnblogs.com/nangongyibin/p/10324263.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!