0

我正在构建一个 android 应用程序,该应用程序需要根据我需要在屏幕上显示的图像数量创建最多 1-4 个图像视图。我写了一个小代码,但它似乎并没有将屏幕平均划分为 4 个图像视图。就我而言,前两个比下两个大一点。以下是我的代码我真的想不出问题是什么

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);      
    }
}
4

1 回答 1

0

您可以将 parentLayout 的 weightsum 设置为等于视图数..并将每个视图的权重设置为 1...这将使每个视图占用相等的空间..

于 2013-07-18T11:56:39.933 回答