效果图:
第一步:创建父布局容器
<LinearLayout android:id="@+id/ll_addView" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" />
第二步:创建item布局
<android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" app:cardCornerRadius="8dp" app:cardElevation="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="right" android:text="序号" /> <TextView android:id="@+id/number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="left" android:padding="5dp" android:text="1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textView_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试项目:" /> <EditText android:id="@+id/ed_one" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@null" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginLeft="64dp" android:background="@color/style_divider_color" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textView_two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="单位:" /> <EditText android:id="@+id/ed_two" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@null" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginLeft="37dp" android:background="@color/style_divider_color" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textView_three" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自动监测仪器测定结果:" /> <EditText android:id="@+id/ed_three" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@null" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginLeft="150dp" android:background="@color/style_divider_color" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textView_four" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="比对方法测定结果1:" /> <EditText android:id="@+id/ed_four" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@null" android:inputType="numberDecimal" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginLeft="130dp" android:background="@color/style_divider_color" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textView_four_s" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="比对方法测定结果2:" /> <EditText android:id="@+id/ed_four_s" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@null" android:inputType="numberDecimal" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginLeft="130dp" android:background="@color/style_divider_color" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textView_five" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="比对监测结果平均值:" /> <TextView android:id="@+id/ed_five_five" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@null" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginLeft="135dp" android:background="@color/style_divider_color" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textView_six" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测定误差(%):" /> <EditText android:id="@+id/ed_six" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@null" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginLeft="103dp" android:background="@color/style_divider_color" /> </LinearLayout> <Button android:id="@+id/btn_addHotel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@null" android:text="+添加" android:textColor="@color/colorAccent" android:textSize="18sp" /> </LinearLayout> </android.support.v7.widget.CardView>
第三步:在activity中实现效果
1:在onCreate方法中调用addItem()
2:实现addItem方法
//添加ViewItem private void addItem(View view) { if (ll_addView.getChildCount() == 0) {//如果一个都没有,就添加一个 View hotelEvaluateView = View.inflate(this, R.layout.item_comparison, null); Button btn_add = (Button) hotelEvaluateView.findViewById(R.id.btn_addHotel); btn_add.setText("+新增"); btn_add.setTag("add"); btn_add.setOnClickListener(this); ll_addView.addView(hotelEvaluateView); sortHotelViewItem(); } else if ((view.getTag()).equals("add")) {//如果有一个以上的Item,点击为添加的Item则添加 View hotelEvaluateView = View.inflate(this, R.layout.item_comparison, null); TextView textView = (TextView) hotelEvaluateView.findViewById(R.id.number); textView.setText((ll_addView.getChildCount() + 1) + ""); ll_addView.addView(hotelEvaluateView); sortHotelViewItem(); } else { sortHotelViewItem(); } }
3:实现sortHoteViewItem()方法
//Item排序 private void sortHotelViewItem() { //获取LinearLayout里面所有的view for (int i = 0; i < ll_addView.getChildCount(); i++) { final View childAt = ll_addView.getChildAt(i); final Button btn_remove = (Button) childAt.findViewById(R.id.btn_addHotel); btn_remove.setText("删除"); btn_remove.setTag("remove");//设置删除标记 btn_remove.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 如果当前点击的view事件为删除,与内存现有的相同,则删除当前点击的Item if (v.getId() == btn_remove.getId()) { ll_addView.removeView(childAt);//从LinearLayout容器中删除当前点击到的ViewItem } } }); //如果是最后一个ViewItem,就设置为添加 if (i == (ll_addView.getChildCount() - 1)) { Button btn_add = (Button) childAt.findViewById(R.id.btn_addHotel); btn_add.setVisibility(View.VISIBLE); btn_add.setText("+新增"); btn_add.setTag("add"); btn_add.setOnClickListener(this); } } }
4:按钮点击事件
@Override public void onClick(View view) { switch (view.getId()) { case R.id.btn_addHotel://点击添加按钮就动态添加Item addViewItem(view); break; } }