例:
A.自定义ViewGroup的方式完成如图一三色梯页面效果,第一条红色,第二条绿色,第三条蓝色,依次循环,台阶上显示第几条台阶数,每台阶梯子占控件宽度的1/3,垂直方向依次向下添加
B.当点击标题栏右上方的添加按钮时,新的台阶添加到三色梯的下方,并显示添加的条数
C.当点击标题栏左上方的删除按钮时,从底部依次删除【删除 添加的时候,使用属性动画】
D.为自定义三色梯条目提供 长按 和 点击 事件,长按删除选中条目,点击则跳转新页面
主方法类:
public class MainActivity extends AppCompatActivity { private Button add_control; private LinearLayout linearlay; private int num = 0; private LinearLayout.LayoutParams mLayoutParams; private View no; private View sure; private AlertDialog.Builder mAlerdialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initview(); mAlerdialog = new AlertDialog.Builder(MainActivity.this); View view = View.inflate(MainActivity.this, R.layout.alertdialog_layout, null); mAlerdialog.setView(view); sure = view.findViewById(R.id.sure); no = view.findViewById(R.id.no); add_control.setOnClickListener(new View.OnClickListener() { @SuppressLint("NewApi") @Override public void onClick(View v) { mLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); num++; final Button mButton = new Button(MainActivity.this); mButton.setId(num); mButton.setText("" + num); mButton.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); mButton.setWidth(240); if (num % 3 == 0) { ObjectAnimator animator = ObjectAnimator.ofFloat(mButton, "translationX", -240, 480); animator.setDuration(3000); animator.setRepeatCount(0); animator.start(); mLayoutParams.setMargins(0, 0, 0, 0); mButton.setLayoutParams(mLayoutParams); mButton.setBackgroundColor(Color.parseColor("#7B68EE")); } else if (num % 3 == 1) { ObjectAnimator animator = ObjectAnimator.ofFloat(mButton, "translationX", -240, 0); animator.setDuration(3000); animator.setRepeatCount(0); animator.start(); mButton.setWidth(240); mLayoutParams.setMargins(0, 0, 0, 0); mButton.setLayoutParams(mLayoutParams); mButton.setBackgroundColor(Color.parseColor("#ADFF2F")); } else if (num % 3 == 2) { ObjectAnimator animator = ObjectAnimator.ofFloat(mButton, "translationX", -240, 240); animator.setDuration(3000); animator.setRepeatCount(0); animator.start(); mLayoutParams.setMargins(0, 0, 0, 0); mButton.setLayoutParams(mLayoutParams); mButton.setBackgroundColor(Color.parseColor("#FF1493")); } mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, Main2Activity.class); Bundle bundle = new Bundle(); bundle.putInt("nums", num); intent.putExtra("bun", bundle); startActivity(intent); } }); linearlay.addView(mButton); mButton.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { num--; mButton.setVisibility(View.GONE); /*mAlerdialog.show(); sure.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { num--; mButton.setVisibility(View.GONE); } }); no.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "取消删除", Toast.LENGTH_SHORT).show(); } });*/ return true; } }); } }); } private void initview() { add_control = (Button) findViewById(R.id.add_contorl); linearlay = (LinearLayout) findViewById(R.id.linearlayout); } }
activity_main 布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" 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" tools:context=".MainActivity"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_weight="8" android:text="三色梯" android:textAlignment="center" android:textSize="25dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_weight="2" android:id="@+id/add_contorl" android:text="+" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:id="@+id/linearlayout" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> </LinearLayout> </LinearLayout>
alertdialog_layout 布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:id="@+id/sure" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="确认" /> <Button android:id="@+id/no" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消" /> </LinearLayout>
添加权限: <application>在这个控件中加入参数(需要跳转的页面)
<activity android:name=".Main2Activity"></activity> </application>
点击跳转的新页面主类:
public class Main2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); } }
activity_main2
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context=".Main2Activity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="11111111111111" /> </RelativeLayout>