android自定义 三色梯,梯形布局

ThreeColorView 
public class ThreeColorView extends ViewGroup {

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

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

    public ThreeColorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    /**
     * 把此view的最终的宽度和高度定下来
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int totalHeight = 0;//此控件的高度
        int totalWidth = 0;//此控件的宽度
        //得到子view数量
        int child = getChildCount();
        if (child > 0) {
            for (int i = 0; i < child; i++) {//遍历子控件
                View view = getChildAt(i);//得到此容器所有的子view
                totalHeight += view.getMeasuredHeight();
                measureChild(view,widthMeasureSpec,heightMeasureSpec);
//                view.measure(widthMeasureSpec, heightMeasureSpec);
            }
        }
        totalWidth = AppUtil.screenWidth(getContext());
        System.out.println("width:"+totalWidth);
        System.out.println("height:"+totalHeight);


        //设置宽度和高度给当前view,通过下面这个方法
        setMeasuredDimension(totalWidth, totalHeight);

    }

    @Override
    protected void onLayout(boolean bo, int left, int top, int right, int bottom) {

        int l = 0;
        int t = 0;
        int r = 0;
        int b = 0;

        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {

            View view = getChildAt(i);//得到每一个view的对象

            view.layout(l, t, l + view.getMeasuredWidth(), t + view.getMeasuredHeight());

            l += view.getMeasuredWidth();

            System.out.println("llll:"+l);

            t += view.getMeasuredHeight();

            if (l+view.getMeasuredWidth()>AppUtil.screenWidth(getContext())){
                l = 0;
            }


            //点击事件
            final int finalI = i;
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getContext(), finalI +":点击位置", Toast.LENGTH_SHORT).show();

                    TextView textView = (TextView) view;

                    Toast.makeText(getContext(), textView.getText().toString() +"文本", Toast.LENGTH_SHORT).show();

                   

                }
            });

            view.setOnLongClickListener(new OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    Toast.makeText(getContext(), finalI +":长按位置", Toast.LENGTH_SHORT).show();

                    removeView(view);
                    return true;
                }
            });



        }


    }
}

ThreeColorActivity

public class ThreeColorActivity extends AppCompatActivity {

    private ThreeColorView threeColorView;
    private int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_threecolor);
        threeColorView = findViewById(R.id.threecolorview);
    }

    /**
     * 添加view
     * @param view
     */
    public void add(View view) {
        count++;

        int width = AppUtil.screenWidth(this);
        TextView textView = new TextView(this);
        textView.setText(count+"");
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(getResources().getColor(R.color.white));

        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView,"translationX",(width-width/3),0);

        objectAnimator.setDuration(3000);
        objectAnimator.start();
        if (count%3==1){
            textView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
        }else if (count%3==2){
            textView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
        }else {
            textView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        }


        threeColorView.addView(textView);

        //得到view的属性参数
        ViewGroup.LayoutParams params = textView.getLayoutParams();
        params.width = width/3;
        params.height = 70;
        textView.setLayoutParams(params);

    }
}


activity_threecolor

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ThreeColorActivity">

    <Button
        android:id="@+id/add"
        android:onClick="add"
        android:text="添加"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <com.example.kson.monthdemo.widget.ThreeColorView
        android:id="@+id/threecolorview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </com.example.kson.monthdemo.widget.ThreeColorView>

</LinearLayout>


猜你喜欢

转载自blog.csdn.net/qq_40788686/article/details/80532649