Android 如何在运行时动态创建ImageView?
我正在构建一个android应用程序,根据需要在屏幕上显示的图像数量,最多需要创建1-4个图像视图。我已经写了一个小代码,但它似乎并没有将屏幕平均划分为4个图像视图。在我的例子中,上面的两个比下面的两个稍微大一点。下面是我的代码,我真的想不出是什么问题Android 如何在运行时动态创建ImageView?,android,android-imageview,android-relativelayout,Android,Android Imageview,Android Relativelayout,我正在构建一个android应用程序,根据需要在屏幕上显示的图像数量,最多需要创建1-4个图像视图。我已经写了一个小代码,但它似乎并没有将屏幕平均划分为4个图像视图。在我的例子中,上面的两个比下面的两个稍微大一点。下面是我的代码,我真的想不出是什么问题 private void CreateScreen() { int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); int screenWidt
private void CreateScreen()
{
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
int matrixN = 2;
int maxWForEachScreen = screenWidth / matrixN;
int maxHForEachScreen = screenHeight / matrixN;
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayoutFullScreen);
ImageView imgView1 = new ImageView(LiveViewer.this);
ImageView imgView2 = new ImageView(LiveViewer.this);
ImageView imgView3 = new ImageView(LiveViewer.this);
ImageView imgView4 = new ImageView(LiveViewer.this);
int size = 3;
if(size >= 3 && size <= 4)
{
imgView1.setImageResource(R.drawable.icon);
imgView2.setImageResource(R.drawable.icon);
imgView3.setImageResource(R.drawable.icon);
imgView4.setImageResource(R.drawable.icon);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(100, 100);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(100, 100);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(100, 100);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(100, 100);
params1.height = maxHForEachScreen/2;
params1.width = maxWForEachScreen/2;
params2.height = maxHForEachScreen/2;
params2.width = maxWForEachScreen/2;
params3.height = maxHForEachScreen/2;
params3.width = maxWForEachScreen/2;
params4.height = maxHForEachScreen/2;
params4.width = maxWForEachScreen/2;
params1.setMargins(0, 0, 0, 0);
params2.setMargins(maxWForEachScreen/2, 0, 0, 0);
params3.setMargins(0, maxHForEachScreen/2, 0, 0);
params4.setMargins(maxWForEachScreen/2, maxHForEachScreen/2, 0, 0);
relativeLayout.addView(imgView1, params1);
relativeLayout.addView(imgView2, params2);
relativeLayout.addView(imgView3, params3);
relativeLayout.addView(imgView4, params4);
}
}
private void CreateScreen()
{
int screenHeight=getWindowManager().getDefaultDisplay().getHeight();
int screenWidth=getWindowManager().getDefaultDisplay().getWidth();
int-matrixN=2;
int maxWForEachScreen=屏幕宽度/matrixN;
int maxHForEachScreen=屏幕高度/matrixN;
RelativeLayout RelativeLayout=(RelativeLayout)findViewById(R.id.relativeLayoutFullScreen);
ImageView imgView1=新的ImageView(LiveViewer.this);
ImageView imgView2=新的ImageView(LiveViewer.this);
ImageView imgView3=新的ImageView(LiveViewer.this);
ImageView imgView4=新的ImageView(LiveViewer.this);
int size=3;
如果(size>=3&&size您可以将parentLayout的权重和设置为视图数..并将1设置为每个视图的权重..这将使每个视图占据相等的空间..尝试使用表格布局或网格布局。使用getSize(Point)
而不是getHeight()
和getWidth())
。它们已被弃用。对于maxWForEachScreen
和maxHForEachScreen
,你得到了什么整数值?@Vikram请详细说明一下?使用相对布局有什么错,我也不认为使用网格布局有什么意义。@Vikram我得到了屏幕宽度和高度的合法值,即1280和720。I think这与布局参数有关,或者因为可能是底部ImageView根据剩余的space@Mr.Noob请看我下面的帖子。