0

例如:我将为星系选项卡做一个布局(使用编辑器)。当我运行银河选项卡模拟器时,我的圈子将散布在它的“屏幕”周围。如果我为较小的手机进行布局,然后运行手机的模拟器,我会得到相同的结果——我的圈子放置在我定义的其他地方。似乎编辑器按照我想要的方式设置了我的圈子 - 相对于它们放置在上面的 imageview。但是,这些设备似乎将圆圈放置在相对于模拟设备屏幕的大小。

我尝试使用 viola_desmond_pic.getWidth() 和 viola_desmond_pic.getHeight() 以便我可以以编程方式布置圆圈,但它们只返回屏幕的大小而不是视图。我了解 dp 和不同密度的规划,但是布局编辑器/模拟器的差异很疯狂。

  1. 还有其他人有这个差异问题吗?我很可能会忽略一些东西。
  2. 如果我决定跳过 XML,如何获得 Imageview 的准确大小“viola_desmond_pic”并从特定边缘以百分比进行布局?

这是我的“layout-xlarge”文件夹中的 XML。我的目标是 Android 2.1

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout04"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView
    android:id="@+id/viola_desmond_pic"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:contentDescription="@string/desc"
    android:scaleType="fitCenter"
    android:src="@drawable/viola_desmond" />

<ImageView
    android:id="@+id/keyhole"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="160dp"
    android:layout_marginTop="95dp"
    android:adjustViewBounds="true"
    android:contentDescription="@string/desc"
    android:scaleType="fitCenter"
    android:src="@drawable/circle3" />

<ImageView
    android:id="@+id/gun"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/keyhole"
    android:layout_alignRight="@+id/cat"
    android:layout_marginRight="42dp"
    android:adjustViewBounds="true"
    android:contentDescription="@string/desc"
    android:paddingBottom="35dp"
    android:paddingLeft="60dp"
    android:paddingRight="50dp"
    android:scaleType="fitCenter"
    android:src="@drawable/circle3" />

<ImageView
    android:id="@+id/popcorn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="61dp"
    android:layout_marginLeft="83dp"
    android:layout_toRightOf="@+id/keyhole"
    android:adjustViewBounds="true"
    android:contentDescription="@string/desc"
    android:paddingBottom="10dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:paddingTop="20dp"
    android:scaleType="fitCenter"
    android:src="@drawable/circle3" />

<ImageView
    android:id="@+id/pen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="24dp"
    android:layout_marginTop="46dp"
    android:adjustViewBounds="true"
    android:contentDescription="@string/desc"
    android:paddingBottom="140dp"
    android:paddingTop="140dp"
    android:src="@drawable/circle3" />


</RelativeLayout>
4

1 回答 1

2

我遇到过类似的问题,即模拟器没有使用正确的资源——即它选择了中等密度并按比例放大,这可能会导致它们出现差异。它值得在文本视图中放入一些描述性文本到每个布局中,并在第一种情况下对此进行测试。

在 ViewTreeObserver 的 preDraw 方法中,您可以尝试使用 iv.getMeasuredHeight 和 getMeasuredWidth。我尽量避免这样做并使用布局。我很确定您不能在 Java 中为布局指定百分比,但是您可以从可用的屏幕尺寸中计算出所需的高度和宽度,并使用 RelativeLayout.LayoutParams 相应地调整图像视图的大小并将参数分配给您的图像视图。这样您就可以非常手动地控制大小。

希望这可以帮助。

杰森

于 2012-03-30T11:18:28.817 回答