3

我有一些坐标信息,在这些坐标上我动态地显示视图。由于 android 设备具有多种分辨率和密度,因此我的视图在高分辨率或低分辨率下变得混乱(这不是根据坐标)。我想在多个屏幕尺寸上使用相同的坐标,而不会弄乱我的视图。我在 android 开发者网站上读到,我们可以使用密度计算来做到这一点,但无法弄清楚。请帮我解决这个问题我真的被困在这一点上。

我还在动态添加视图的地方附加了我的代码。

for (int layoutIndex = 0; layoutIndex < layoutSize; layoutIndex++) {
        SubMenuItems coorObj = submenuCoordinateList.get(layoutIndex);

        System.out.println("X coordinate= " + coorObj.xCoordinate.trim());
        System.out.println("Y coordinate= " + coorObj.yCoordinate.trim());

        subMenuLayout[layoutIndex] = new LinearLayout(this);
        subMenuImageView[layoutIndex] = new ImageView(this);
        subMenuResultView[layoutIndex] = new TextView(this);
        subMenuNameView[layoutIndex] = new TextView(this);

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        RelativeLayout.LayoutParams nameParams = new RelativeLayout.LayoutParams(
                80, LayoutParams.WRAP_CONTENT);

        layoutParams.leftMargin = Integer.parseInt(coorObj.xCoordinate
                .trim());
        layoutParams.topMargin = Integer.parseInt(coorObj.yCoordinate
                .trim());

        subMenuLayout[layoutIndex].setLayoutParams(layoutParams);
        subMenuNameView[layoutIndex].setLayoutParams(nameParams);

        subMenuNameView[layoutIndex].setTextSize(
                TypedValue.COMPLEX_UNIT_SP, 10);
        subMenuResultView[layoutIndex].setTextSize(
                TypedValue.COMPLEX_UNIT_SP, 10);

        subMenuImageView[layoutIndex]
                .setImageResource(SubMenuItems.resSubMenuImages[layoutIndex]);
        subMenuNameView[layoutIndex]
                .setText(SubMenuItems.resSubMenuName[layoutIndex]);
        subMenuResultView[layoutIndex].setText(menuId + " Result");

        subMenuNameView[layoutIndex].setGravity(Gravity.CENTER);
        subMenuResultView[layoutIndex].setGravity(Gravity.CENTER);

        subMenuLayout[layoutIndex].addView(subMenuImageView[layoutIndex]);
        subMenuLayout[layoutIndex].addView(subMenuResultView[layoutIndex]);
        subMenuLayout[layoutIndex].addView(subMenuNameView[layoutIndex]);

        submenu_icons_layout.addView(subMenuLayout[layoutIndex],
                layoutIndex);
    }
4

0 回答 0