android 代码添加一个4040的ImageView代码
以下是在Android中添加一个40*40的ImageView的代码示例:
// 创建ImageView对象
ImageView imageView = new ImageView(context);
// 设置ImageView的宽度和高度
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(40, 40);
imageView.setLayoutParams(layoutParams);
// 设置ImageView的背景(可选)
imageView.setBackgroundResource(R.drawable.image_background);
// 将ImageView添加到布局中
LinearLayout linearLayout = findViewById(R.id.linearLayout); // 假设你有一个LinearLayout作为容器
linearLayout.addView(imageView);
注意,上述代码中的context应该替换为你的上下文对象,R.drawable.image_background应该替换为你想要设置的ImageView背景资源。此外,你需要确保在布局文件中有一个LinearLayout容器(例如,使用LinearLayout作为根元素)
原文地址: https://www.cveoy.top/t/topic/h8tj 著作权归作者所有。请勿转载和采集!