【问题标题】:Android Emulator doesn't match phoneAndroid 模拟器与手机不匹配
【发布时间】:2011-02-13 03:34:12
【问题描述】:

我有一个使用 Canvas 绘图原语在自定义视图中绘制图形的布局,以及一个 TextView 和 2 个按钮。模拟器设置为与手机相同的像素宽度(400 像素),并且 2D 图形以像素尺寸指定。

(左边是模拟器,右边是电话。)

注意图形匹配。 (由于数据不同,图形的实际内容也不同)但是TextView和按钮是完全不同的。这里是XML -

    <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:background="#303080"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"> 
   <TextView android:id="@+id/eventSummary" 
    android:layout_width="250dp"
    android:background="#202090"    
    android:layout_height="fill_parent" 
    android:minLines="4" 
    android:singleLine="false"
    android:text="text" />
   <Button android:id="@+id/PrevButton"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:onClick="prevButtonHandler"
    android:text="&lt;"
   />
   <Button android:id="@+id/NextButton"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="nextButtonHandler"
    android:text="&gt;"
   /> 
 </LinearLayout>

...我尝试使用 SP 和 DP 指定 TextView 的宽度。为什么会有这么大的差异,我怎样才能让模拟器和手机匹配? (FWIW 手机是 HTC Droid Incredible)。

提前致谢!

【问题讨论】:

  • 并非所有模拟器都是完美的。也许将此作为​​错误报告提交给模拟器的制造商。
  • 只是普通的安卓模拟器。此外,2D 图形匹配,TextView 宽度(250)在模拟器上看起来比在手机上更正确。我需要了解为什么会有如此大的差异。

标签: android


【解决方案1】:

尝试在LinearLayout 中设置layout_width 中的dp,然后在TextView 中使用android:layout_weight="1"

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:background="#303080"
    android:layout_width="280dp"
    android:layout_height="wrap_content"> 
    <TextView android:id="@+id/eventSummary" 
        android:layout_width="wrap_content"
        android:background="#202090"    
        android:layout_height="fill_parent" 
        android:layout_weight="1"
        android:minLines="4" 
        android:singleLine="false"
        android:text="text" />
    <Button android:id="@+id/PrevButton"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:onClick="prevButtonHandler"
        android:text="&lt;"
    />
    <Button android:id="@+id/NextButton"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:onClick="nextButtonHandler"
        android:text="&gt;"
    /> 
</LinearLayout>

【讨论】:

  • 这产生了与原始结果基本相同的结果。
  • @Peter Nelson 所以我建议的 xml 在模拟器 设备中产生了与原始 xml 相同的结果?
  • 是的。但是我通过使用 TableLayout 而不是 LinearLayout 解决了我的问题。这有一个额外的优势,我可以在不编写任何文字宽度的情况下做到这一点 - 我可以单独使用权重来保持一切正常。
  • @Peter Nelson 我的下一个建议是单独使用layout_weight 而不使用dp 值来分隔屏幕上的元素。虽然需要额外的LinearLayout。很高兴你成功了。
【解决方案2】:

我迟到了,但可能是因为像素密度。查看此链接,看看是否有帮助:http://developer.android.com/guide/practices/screens_support.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 2023-03-15
    • 1970-01-01
    • 2011-06-15
    相关资源
    最近更新 更多