梯形布局

public class tixing extends ViewGroup{

    private int measuredWidth;

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

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measuredWidth = getMeasuredWidth();
        //测量子View的宽高,只有ViewFroup中有这个方法,直接继承自View是没有这个方法的
        measureChildren(widthMeasureSpec,heightMeasureSpec);

    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {

        //拿到子控件的个数
        int childCount = getChildCount();
        //定义一个临时高度
        int startHeight=0;
        int startWidth=0;
        //循环遍历出每一个View
        for(int a=0;a<childCount;a++){
            final View v = getChildAt(a);
            //给每一个view设置自己的位置  上  右  下  左
                v.layout(startWidth, startHeight, startWidth + v.getMeasuredWidth(), startHeight + v.getMeasuredHeight());
                startHeight += v.getMeasuredHeight();
                startWidth += v.getMeasuredWidth();
            if (startWidth>=measuredWidth) {
                startWidth=0;
            }
        }
    }
    
}
//添加布局
i++;
final TextView textView = new TextView(MainActivity.this);
textView.setText(i+"");
if (i<10) {
    textView.setTag("0"+i);
}else
    {
        textView.setTag(i);
    }
textView.setHeight(100);
textView.setWidth(screenWidth/3);
if (i%3==1){
    textView.setBackgroundColor(Color.RED);
}else if (i%3==2)
{
    textView.setBackgroundColor(Color.BLUE);
}else
    {
        textView.setBackgroundColor(Color.BLACK);
    }
tixing.addView(textView);
ObjectAnimator animatorX = ObjectAnimator.ofFloat(textView, "translationX", -800,0);
animatorX.start();

//点击事件 加传值
textView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(MainActivity.this,Main2Activity.class);
        intent.putExtra("aaa",textView.getTag()+"");
        Toast.makeText(MainActivity.this,textView+"",Toast.LENGTH_SHORT).show();
        startActivity(intent);
    }
});
//长按删除
textView.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View view) {
        ObjectAnimator animatorX = ObjectAnimator.ofFloat(textView, "alpha", 1,0);
        animatorX.start();
@Override
public void onAnimationEnd(Animator animator) {
    tixing.removeView(textView);
}
return true



猜你喜欢

转载自blog.csdn.net/wumeng5211314/article/details/80523363