Android动态添加和删除布局详解(及demo)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/yuhang01/article/details/102623537

因公司需求的增加,需要我进行扫码完成后动态的添加数据到相同的布局中
效果图是这样的:
在这里插入图片描述在这里插入图片描述
其中,点击扫码按钮的时候进行条形码的扫描以及添加他们共有的字段
共有的布局如下:
在这里插入图片描述
fahuoadditem.layout

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

    <TextView
        android:layout_width="match_parent"
        android:text="快递公司:"
        android:padding="5dp"
        android:textColor="#000"
        android:layout_height="wrap_content" />
    <Spinner
        android:id="@+id/power_add_spinner"
        android:background="#EDEFF2"
        android:layout_margin="5dp"
        android:layout_width="match_parent"
        android:layout_height="40dp"></Spinner>

    <EditText
        android:hint="快递单号文本框"
        android:id="@+id/fahuoremind_add_edit"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#FFF"
        />
    <Button
        android:id="@+id/item_remove"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="del"
        android:text="删除"
        />
</LinearLayout>

回归重点:如何进行动态的增加布局呢
这里就用到LayoutInflater将共有布局添加到我们当前的布局中

/**
inflate参数详解
* 参数1:需要动态添加的布局文件
* 参数2:父布局容器
* 参数3:false表示没有把布局文件添加到父布局中,true表示把布局文件添加到容器中
*/

final View inflateView = LayoutInflater.from(FahuoremidAdd.this).inflate(R.layout.fahuoadditem, linearLayout, false);

删除的话就用到的时

linearLayout.removeView(inflateView);

其中我在引用子项布局进行初始化的时候的点击事件报错 (怪自己粗心啦)

总的代码:
FahuoremidAdd

public class FahuoremidAdd extends AppCompatActivity {
    private static final int REQUEST_QR_CODE = 1;
    private static final int REQUEST_QR_TYPE = 2;
    private EditText fahuodan;
    private LinearLayout linearLayout;
    private EditText text;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fahuoremid_add);
        fahuodan = findViewById(R.id.fahuoremind_add_edit);
        linearLayout = findViewById(R.id.fahuo_add_fu);
    }

    public void saowo(View view){//动态添加按钮
 
          /*// 第三个参数设置为false + addView -> 可以把布局文件添加到父布局中*/
        final View inflateView = LayoutInflater.from(FahuoremidAdd.this).inflate(R.layout.fahuoadditem, linearLayout, false);
        linearLayout.addView(inflateView);
        Button remove = inflateView.findViewById(R.id.item_remove);
        text = inflateView.findViewById(R.id.fahuoremind_add_edit);
       remove.setOnClickListener(new OnClickListener() {//布局中进行点击事件动态删除
           @Override
           public void onClick(View view) {
              linearLayout.removeView(inflateView);
           }
       });

    }
}

activity_fahuoremid_add.layout

<?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"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FahuoremidAdd">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="#007FFF">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:padding="15dp"
            android:text="发货单添加"
            android:textColor="#FFF"
            android:textSize="18dp" />
    </RelativeLayout>
     <ScrollView
         android:layout_width="match_parent"
         android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/fahuo_add_fu"
        android:orientation="vertical"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="match_parent"
            android:text="发货单:"
            android:padding="5dp"
            android:textColor="#000"
            android:layout_height="wrap_content" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="发货单扫码"
    android:onClick="sao"
    />
    <EditText
        android:id="@+id/fahuoremind_add_edit"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#FFF"
        />
    <View
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#CDCDCD"
        />
    <TextView
        android:layout_width="match_parent"
        android:text="快递单:"
        android:padding="5dp"
        android:textColor="#000"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="扫码"
        android:onClick="saowo"
        />

    </LinearLayout>
     </ScrollView>
</LinearLayout>

共有的布局在上面
ok这样就完成啦 nice

猜你喜欢

转载自blog.csdn.net/yuhang01/article/details/102623537