如何根据屏幕尺寸或密度更改X,Y坐标?

时间:2013-05-06 11:42:04

标签: android multiscreen

我有一些坐标信息,在这些坐标上我动态显示视图。由于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);
    }

0 个答案:

没有答案