【问题标题】:Relative Layout gets messed up in different emulators相对布局在不同的模拟器中变得混乱
【发布时间】:2015-10-29 02:39:02
【问题描述】:

所以我正在为我的第一个应用程序设计我的主页,当我模拟它时,我注意到我的秒表的尺寸由于某种原因变得更大了。所以我拿起我的手机在那里测试了它,结果并不漂亮。我使用了 dp,我几乎可以肯定它是 % 而不是设置值,所以我不知道它为什么会这样变化。

这是图片,所以它不会弄乱我的问题http://imgur.com/a/IKvdI

menu1_layout.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:gravity="center_horizontal">

        <ImageView
            android:layout_width="500dp"
            android:layout_height="800dp"
            android:id="@+id/imageView"
            android:background="@drawable/background3" />

    </RelativeLayout>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:gravity="center_horizontal">

    <Button
        android:id="@+id/stopWatch"
        android:layout_width="225dp"
        android:layout_height="200dp"
        android:background="@drawable/stopwatch2"
        android:layout_marginTop="83dp"
        android:layout_marginRight="3dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:gravity="center_horizontal"
        android:layout_gravity="center_horizontal|bottom">

    <Chronometer
        android:id="@+id/aChronometer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        android:textColor="#ffffff"
        android:layout_marginBottom="144dp"
        android:layout_above="@+id/editText"
        android:layout_centerHorizontal="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:text="@string/Explanation"
            style="@style/Base.TextAppearance.AppCompat.Medium"
            android:layout_above="@+id/button"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="26dp"
            android:textColor="#ffffff" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText2"
            android:text="= 0"
            android:layout_marginTop="8dp"
            style="@style/Base.TextAppearance.AppCompat.Medium"
            android:layout_alignTop="@+id/button"
            android:layout_centerHorizontal="true"
            android:layout_marginRight="-10dp"
            android:textColor="#ffffff" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="50dp"
            android:layout_height="40dp"
            android:background="@drawable/cookie"
            android:id="@+id/button"
            android:layout_marginRight="-10dp"
            android:layout_alignParentBottom="true"
            android:layout_toLeftOf="@+id/editText2"
            android:layout_toStartOf="@+id/editText2"
            android:layout_marginBottom="84dp" />


    </RelativeLayout>
</FrameLayout>

【问题讨论】:

  • 额外评论我最终将所有不必要的按钮更改为 imageview,但秒表必须是一个按钮,这就是我如何修复它的地方。 edit 我看到了权重和其他方法,但我不知道如何实现。有没有办法为某些对象创建设置大小,所以当背景变宽时,我的图像也会变宽。

标签: android layout


【解决方案1】:

如果您在 5 种不同尺寸的不同设备上运行特定布局,您将获得 5 种不同的结果。

嗯,这是因为您为layout_heightlayout_widthmargins 设置了特定值,例如225dp200dp。我知道这些是independent pixels,但是当考虑可绘制时,永远不要将widthheight 设置为特定值。它弄乱了纵横比。这些设置适用于设备的一种特定配置,但不适用于其他设备。设备会根据设备的分辨率选择不同的drawable。

你需要尝试设计你的相对于其他视图。始终确保layout_widthlayout_hightwrap_content 中。这将确保drawable 在所有设备和模拟器中以正确的纵横比加载。

【讨论】:

  • 哦,好的,谢谢,我打算做的是编辑背景和其他对象,这样无论它们在哪里都不会弄乱。我将尝试使用 alignParent 而不是触摸边距/宽度。所以我对图像进行了Photoshop处理,以便于我。
  • 好的,我解决了。我将相对布局放在线性中,几乎哭了,但我终于弄清楚了它是如何使用重量、重量总和、重力和反复试验的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-30
  • 1970-01-01
  • 2020-08-23
  • 2015-04-13
  • 2021-02-12
相关资源
最近更新 更多