往LinearLayout中动态增/删子view


    想要在LinearLayout布局中添加ImageView并设置view的大小、添加TextView并设置文字大小等,可以参考下方的实现方式!

添加到指定的位置

// ImageView
ImageView child = new ImageView(getApplicationContext());
child.setImageResource(R.mipmap.ic_launcher_round);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300, 300);
child.setLayoutParams(layoutParams);
linear_layout.addView(child, Integer.valueOf(add_index) - 1);

// TextView
TextView child2 = new TextView(getApplicationContext());
child2.setTextSize(30);
child2.setText("hello " + System.currentTimeMillis());
linear_layout.addView(child2, Integer.valueOf(add_index));

添加到最后

TextView child2 = new TextView(getApplicationContext());
child2.setTextSize(30);
child2.setText("hello "+ System.currentTimeMillis());
linear_layout.addView(child2);

删除指定位置

View childAt = linear_layout.getChildAt(Integer.valueOf(remove_index) - 1);
linear_layout.removeView(childAt);

删除最后一个

View childAt = linear_layout.getChildAt(linear_layout.getChildCount() - 1);
linear_layout.removeView(childAt);

删除所有

linear_layout.removeAllViews();

完整代码

xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <EditText
                android:id="@+id/add_index_et"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:hint="添加到第几个"
                />
            <Button
                android:id="@+id/add_index_bt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="添加指定位置"/>
        </LinearLayout>
        <Button
            android:id="@+id/add_bt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加到最后的位置"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <EditText
                android:id="@+id/remove_index_et"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:hint="删除第几个"
                />
            <Button
                android:id="@+id/remove_index_bt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="删除指定位置"/>
        </LinearLayout>
        <Button
            android:id="@+id/removeLast_bt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除最后一个"/>
        <Button
            android:id="@+id/addAll_bt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除所有"/>

        <LinearLayout
            android:id="@+id/linear_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
           />
    </LinearLayout>
</ScrollView>

Activity:

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    LinearLayout linear_layout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        linear_layout = findViewById(R.id.linear_layout);

        // 添加到指定位置
        Button add_index_bt = findViewById(R.id.add_index_bt);
        add_index_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText add_index_et = findViewById(R.id.add_index_et);
                String add_index = add_index_et.getText().toString().trim();

                if (!TextUtils.isEmpty(add_index)) {
                    ImageView child = new ImageView(getApplicationContext());
                    child.setImageResource(R.mipmap.ic_launcher_round);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300, 300);
                    child.setLayoutParams(layoutParams);
                    linear_layout.addView(child, Integer.valueOf(add_index) - 1);

                    TextView child2 = new TextView(getApplicationContext());
                    child2.setTextSize(30);
                    child2.setText("hello " + System.currentTimeMillis());
                    linear_layout.addView(child2, Integer.valueOf(add_index));
                }
            }
        });

        // 添加到末尾
        Button add_bt = findViewById(R.id.add_bt);
        add_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageView child = new ImageView(getApplicationContext());
                child.setImageResource(R.mipmap.ic_launcher_round);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300, 300);
                child.setLayoutParams(layoutParams);
                linear_layout.addView(child);

                TextView child2 = new TextView(getApplicationContext());
                child2.setTextSize(30);
                child2.setText("hello "+ System.currentTimeMillis());
                linear_layout.addView(child2);
            }
        });

        // 删除指定位置的imageView
        Button remove_index_bt = findViewById(R.id.remove_index_bt);
        remove_index_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText remove_index_et = findViewById(R.id.remove_index_et);
                String remove_index = remove_index_et.getText().toString().trim();
                if (!TextUtils.isEmpty(remove_index)) {
                    if ( Integer.valueOf(remove_index) <= linear_layout.getChildCount() ){
                        View childAt = linear_layout.getChildAt(Integer.valueOf(remove_index) - 1);
                        linear_layout.removeView(childAt);
                    }
                }
            }
        });

        // 删除最后一个
        Button removeLast_bt = findViewById(R.id.removeLast_bt);
        removeLast_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (linear_layout.getChildCount() > 0){
                    View childAt = linear_layout.getChildAt(linear_layout.getChildCount() - 1);
                    linear_layout.removeView(childAt);
                }
            }
        });

        // 删除所有子view
        Button addAll_bt = findViewById(R.id.addAll_bt);
        addAll_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                linear_layout.removeAllViews();
            }
        });
    }
}

效果

添加view演示效果

仅供参考!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值