【问题标题】:Layout looks different on devices布局在设备上看起来不同
【发布时间】:2014-10-30 06:13:35
【问题描述】:

嗨,我的应用在不同的设备上看起来不同。我使用 Samsung Galagy S2 作为我的测试设备,但是当我尝试将应用程序安装到另一台设备三星 Galaxy Note 3 时。

显示分辨率: 三星 Galaxy S2:800x480(hdp) 三星galaxz Note 3:1920x1080(xxhdp)

我的应用在: 三星galaxy S2赞THIS 三星galaxy Note 3赞THIS

正如您在第一张图片中看到的那样,它看起来不错,但在 Note 3 上,应用程序底部有很多空白空间。我使用的所有图像也是 xxhdp 和 hdp。问题在于按钮之间的间隙。我将它设置在 dp 中,所以我认为在每个设备中它都会像我的手机一样。但在 Galaxy S2 和 Galaxy Note 3 上,它看起来是一样的差距。

这是我的布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayoutHA"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/background" >

    <RelativeLayout
        android:id="@+id/relativeLayoutHA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/infoButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="0dp"
            android:minWidth="0dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/info"
            />

        <Button
            android:id="@+id/facebookButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="0dp"
            android:minWidth="0dp"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/infoButton"
            android:layout_marginRight="5dp"
            android:background="@drawable/facebook"
             />

         <Button
            android:id="@+id/emailButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="0dp"
            android:minWidth="0dp"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/facebookButton"
            android:layout_marginRight="5dp"
            android:background="@drawable/email"
             />

    </RelativeLayout>

    <Button
        android:id="@+id/animal"
        android:layout_width="200dp"
        android:layout_height="55dp"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:background="@drawable/shape_button"
        android:drawableLeft="@drawable/animal_pic"
        android:text="Animals" />

    <Button
        android:id="@+id/insect"
        android:layout_width="200dp"
        android:layout_height="55dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:background="@drawable/shape_button"
        android:drawableLeft="@drawable/insect"
        android:text="Insects" />

    <Button
        android:id="@+id/nature"
        android:layout_width="200dp"
        android:layout_height="55dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:background="@drawable/shape_button"
        android:drawableLeft="@drawable/nature"
        android:text="Nature" />

    <Button
        android:id="@+id/people"
        android:layout_width="200dp"
        android:layout_height="55dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:background="@drawable/shape_button"
        android:drawableLeft="@drawable/people"
        android:text="People" />

    <Button
        android:id="@+id/gun"
        android:layout_width="200dp"
        android:layout_height="55dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:background="@drawable/shape_button"
        android:drawableLeft="@drawable/gun"
        android:text="Guns" />



</LinearLayout>

【问题讨论】:

  • 两张图片相同。
  • 不,我在 xxhdp 中有 260x260,在 hdp 中有 130x130
  • 我是说你上传的图片是一样的。
  • 发生这种情况是因为您将垂直按钮的 layout_height 固定为相同的值 (55dp)。在 xxhdpi 中,设备高度较长,因此您的应用底部有很多空白空间
  • 你可以使用带有权重属性的线性布局

标签: android android-layout


【解决方案1】:

您应该使用 LinearLayout 来覆盖所有垂直按钮,并将此 LinearLayout 的 layout_height 设置为 match_parent。在此 LinearLayout 中,将每个按钮的 layout_weight 设置为相同的值(例如:1),并将 layout_height 设置为 0dp

【讨论】:

    【解决方案2】:

    您必须使用 dimen 管理高度和宽度。为 values-v11、values-v14、values-sw600dp 值布局创建一个维度,并根据布局和检查设置高度和宽度。

    【讨论】:

      【解决方案3】:

      我也有同样的问题,但我通过为同一个活动创建 3 个不同的布局来解决它。

      1. layout_large(用于 xhdp 布局)
      2. layout_xlarge(用于 xxhdp 布局)
      3. 布局(用于 xhdp 布局)

      您无需担心分配首选布局,Android 设备会选择最适合其屏幕的布局。我的回答似乎是额外的工作,但如果您想在不同的屏幕尺寸上获得一致的视图,这是最可取的。阅读documentation

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-20
        • 2018-07-03
        • 1970-01-01
        相关资源
        最近更新 更多