简体   繁体   English

在布局编辑器中布局看起来很好 - 完全不在仿真设备上

[英]layout looks good in android layout editor - completely off on emulated devices

For example: I'll do a layout (using the editor) for the galaxy tab. 例如:我将为galaxy选项卡进行布局(使用编辑器)。 When I run galaxy tab emulator my circles will be strewn all around the its "screen". 当我运行galaxy tab模拟器时,我的圆圈将遍布其“屏幕”。 If I do a layout for a smaller phone and then run the phone's emulator, I get the same results - my circles placed in other places than I defined. 如果我为较小的手机进行布局然后运行手机的模拟器,我会得到相同的结果 - 我的圈子放在我定义的其他地方。 It seems like the editor sets my circles like I want - relative to imageview they are placed on top of. 好像编辑器像我想要的那样设置我的圈子 - 相对于imageview,它们被置于顶部。 However, the devices seem to place the circles relative to the size of the emulated device's screen. 但是,设备似乎将圆圈放置在相对于模拟设备屏幕大小的位置。

I've tried to use viola_desmond_pic.getWidth() and viola_desmond_pic.getHeight() so that I can lay out the circles programatically but they just return the size of the screen rather than the view. 我试图使用viola_desmond_pic.getWidth()和viola_desmond_pic.getHeight(),以便我可以以编程方式布置圆圈,但它们只返回屏幕的大小而不是视图。 I understand dp and planning for different densities but, the layout editor/emulator discrepancy is crazy. 我理解dp并计划不同的密度但是,布局编辑器/模拟器的差异是疯狂的。

  1. Does anyone else have this discrepancy problem? 有没有其他人有这个差异问题? I could very well be overlooking something. 我很可能会忽略一些东西。
  2. How could I get can accurate size of the Imageview, "viola_desmond_pic" and do my layout from particular edges with percentages if I decide to skip XML? 如果我决定跳过XML,我怎么能得到Imageview的准确大小,“viola_desmond_pic”并从百分比的特定边缘做我的布局?

Here's the XML from my "layout-xlarge" folder. 这是我的“layout-xlarge”文件夹中的XML。 I'm targeting Android 2.1 我的目标是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>

I have had similar issues where the emulator does not use the correct resources - ie it picks medium density and scales up which can cause they discrepancies. 我有类似的问题,模拟器没有使用正确的资源 - 即它选择中密度和扩大,这可能导致它们的差异。 Its worth dropping in a textview with some descriptive text into each layout and testing for this in the first case. 它值得在textview中删除,在每个布局中都有一些描述性文本,并在第一种情况下对此进行测试。

In the preDraw method of the ViewTreeObserver, you could try using iv.getMeasuredHeight and getMeasuredWidth. 在ViewTreeObserver的preDraw方法中,您可以尝试使用iv.getMeasuredHeight和getMeasuredWidth。 I try to steer clear of doing this though and play with the layouts. 我尝试避免这样做,然后玩布局。 Im quite sure you cant specify percentages in Java for layout, however you could work out the desired height and width from the available screen size and size your imageviews accordingly using RelativeLayout.LayoutParams and assigning the params to your imageview. 我很确定你不能在Java中指定布局的百分比,但是你可以从可用的屏幕尺寸计算出所需的高度和宽度,并使用RelativeLayout.LayoutParams相应地调整图像视图的大小,并将参数分配给你的imageview。 This way you have very manual control over size. 这样您就可以非常手动控制尺寸。

Hope this helps. 希望这可以帮助。

Jason 贾森

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM