📜  如何在Android中通过单击按钮动态创建新的ImageView?

📅  最后修改于: 2021-05-08 19:36:05             🧑  作者: Mango

在本文中,我们将实现与ImageView相关的非常重要的功能。在这里,我们将动态添加ImageView。我们将仅更改背景颜色。每当我们单击按钮时,都会创建一个新的ImageView。因此,在这里我们将学习如何实现该功能。下面提供了一个示例视频,以使您对本文中的工作有个大概的了解。注意,我们将使用Java语言实现该项目。

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。

步骤2:使用activity_main.xml文件

导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。

XML


      
    
    


Java
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
  
import androidx.appcompat.app.AppCompatActivity;
  
import java.util.Random;
  
public class MainActivity extends AppCompatActivity {
  
    Button addview;
    LinearLayout layout;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // initialising layout
        addview = findViewById(R.id.addiview);
        layout = findViewById(R.id.layout);
          
        // we will click on the add view button
        addview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  
                // initialising new layout
                ImageView imageView = new ImageView(MainActivity.this);
                  
                // setting the image in the layout
                imageView.setImageResource(R.mipmap.ic_launcher);
                  
                // calling addview with width and height
                addvieW(imageView, 200, 200);
  
                // adding the background color
                colorrandom(imageView);
            }
        });
    }
  
    public void colorrandom(ImageView imageView) {
  
        // Initialising the Random();
        Random random = new Random();
          
        // adding the random background color
        int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));
          
        // setting the background color
        imageView.setBackgroundColor(color);
    }
  
    private void addvieW(ImageView imageView, int width, int height) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
          
        // setting the margin in linearlayout
        params.setMargins(0, 10, 0, 10);
        imageView.setLayoutParams(params);
          
        // adding the image in layout
        layout.addView(imageView);
    }
}


步骤3:使用MainActivity。 Java文件

转到MainActivity。 Java文件并参考以下代码。在这里,我们将创建一个ImageView和一个布局,并将ImageView添加到布局中。这就是我们创建ImageView的方式

ImageView imageView=new ImageView(AddImageViw.this);
// adding the image in ImageView
imageView.setImageResource(R.mipmap.ic_launcher);

这就是我们在布局中添加新创建的图像视图的方式。

LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(width,height);
        params.setMargins(0,10,0,10); // setting the margin in the layout 
        imageView.setLayoutParams(params);
        layout.addView(imageView);    // adding the image in the layout

以下是MainActivity完整代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
  
import androidx.appcompat.app.AppCompatActivity;
  
import java.util.Random;
  
public class MainActivity extends AppCompatActivity {
  
    Button addview;
    LinearLayout layout;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // initialising layout
        addview = findViewById(R.id.addiview);
        layout = findViewById(R.id.layout);
          
        // we will click on the add view button
        addview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  
                // initialising new layout
                ImageView imageView = new ImageView(MainActivity.this);
                  
                // setting the image in the layout
                imageView.setImageResource(R.mipmap.ic_launcher);
                  
                // calling addview with width and height
                addvieW(imageView, 200, 200);
  
                // adding the background color
                colorrandom(imageView);
            }
        });
    }
  
    public void colorrandom(ImageView imageView) {
  
        // Initialising the Random();
        Random random = new Random();
          
        // adding the random background color
        int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));
          
        // setting the background color
        imageView.setBackgroundColor(color);
    }
  
    private void addvieW(ImageView imageView, int width, int height) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
          
        // setting the margin in linearlayout
        params.setMargins(0, 10, 0, 10);
        imageView.setLayoutParams(params);
          
        // adding the image in layout
        layout.addView(imageView);
    }
}

输出: