以下是在Android中添加一个40*40的ImageView的代码示例:\n\njava\n// 创建ImageView对象\nImageView imageView = new ImageView(context);\n\n// 设置ImageView的宽度和高度\nLinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(40, 40);\nimageView.setLayoutParams(layoutParams);\n\n// 设置ImageView的背景(可选)\nimageView.setBackgroundResource(R.drawable.image_background);\n\n// 将ImageView添加到布局中\nLinearLayout linearLayout = findViewById(R.id.linearLayout); // 假设你有一个LinearLayout作为容器\nlinearLayout.addView(imageView);\n\n\n注意,上述代码中的context应该替换为你的上下文对象,R.drawable.image_background应该替换为你想要设置的ImageView背景资源。此外,你需要确保在布局文件中有一个LinearLayout容器(例如,使用LinearLayout作为根元素)。


原文地址: https://www.cveoy.top/t/topic/pRgi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录