自定义view
public class MyThereColorView extends ViewGroup { public MyThereColorView(Context context) { super( context ); } public MyThereColorView(Context context, AttributeSet attrs) { super( context, attrs ); } public MyThereColorView(Context context, AttributeSet attrs, int defStyleAttr) { super( context, attrs, defStyleAttr ); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure( widthMeasureSpec, heightMeasureSpec ); int totalHeight = 0; int totalWidth = 0; int childCount = getChildCount(); if (childCount > 0) { for (int i = 0; i < childCount; i++) { View v = getChildAt( i ); totalHeight += v.getMeasuredHeight(); measureChild( v, widthMeasureSpec, heightMeasureSpec ); } } totalWidth = AppUtil.screenWidth( getContext() ); setMeasuredDimension( totalWidth, totalHeight ); } @Override protected void onLayout(boolean changed, 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++) { final View view = getChildAt( i ); view.layout( l, t, l + view.getMeasuredWidth(), t + view.getMeasuredHeight() ); l += view.getMeasuredWidth(); t += view.getMeasuredHeight(); if (l + view.getMeasuredWidth() > AppUtil.screenWidth( getContext() )) { l = 0; } final int finalI = 0; view.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { Toast.makeText( getContext(), finalI + ":点击位置", Toast.LENGTH_SHORT ).show(); TextView textView = (TextView) view; Toast.makeText( getContext(), textView.getText().toString(), Toast.LENGTH_SHORT ).show(); Intent intent = new Intent( getContext(), MainActivity.class ); intent.putExtra( "id", textView.getText().toString() ); getContext().startActivity( intent ); } } ); view.setOnLongClickListener( new OnLongClickListener() { @Override public boolean onLongClick(View v) { Toast.makeText(getContext(), finalI +":长按位置", Toast.LENGTH_SHORT).show(); removeView( v ); return true; } } ); } }
}
activity
public class MyThereColorViewActivity extends AppCompatActivity { int count = 0; private MyThereColorView myThereColorView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_my_there_color_view ); Button button = findViewById( R.id.bt ); myThereColorView = findViewById( R.id.mtcv ); button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { count++; int i = AppUtil.screenWidth( MyThereColorViewActivity.this ); TextView textView = new TextView( MyThereColorViewActivity.this ); textView.setText( count + "" ); textView.setGravity( Gravity.CENTER ); textView.setTextColor(getResources().getColor(R.color.white)); ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView,"translationX",(i-i/3),0); objectAnimator.setDuration(3000); objectAnimator.start(); if (count==1||count==4||count==7||count==10||count==13||count==16){ textView.setBackgroundColor(getResources().getColor(R.color.colorAccent)); }else if (count==2||count==5||count==8||count==11||count==14){ textView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark)); }else if (count==3||count==6||count==9||count==12||count==15){ textView.setBackgroundColor(getResources().getColor(R.color.colory)); } myThereColorView.addView(textView); //得到view的属性参数 ViewGroup.LayoutParams params = textView.getLayoutParams(); params.width = i/3; params.height = 70; textView.setLayoutParams(params); } } ); } }