【问题标题】:RelativeLayout with background image scales incorrectly具有背景图像的 RelativeLayout 缩放不正确
【发布时间】:2015-06-16 13:05:40
【问题描述】:

我有一个带有背景图片的RelativeLayout。在其中,我有另一个 RelativeLayout 带有背景颜色。我希望第二个 RelativeLayout 位于第一个的中间,但根据屏幕尺寸,它的比例尺是错误的。

代码如下:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp">

    <RelativeLayout
        android:id="@+id/cellphone"
        android:layout_marginLeft="80dp"
        android:layout_marginBottom="120dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:background="@drawable/cellphone">

        <RelativeLayout
            android:id="@+id/cellphone_screen"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="40dp"
            android:layout_marginBottom="40dp"
            android:background="#e34579">

        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>

大屏设备截图:

相同代码的屏幕截图,但是是小屏幕设备:

如您所见,黄色边框不应该是可见的。

我该如何解决?

P.S.:我有各种尺寸的背景图片 (cellphone.png)(从 drawable-ldpi 到 drawable-xxxhdpi)。

编辑:

xxhdpi 中的 cellphone.png 图像:

【问题讨论】:

  • 你能提供具体的图片吗?
  • 请出示手机.png
  • @RishiPaul,这是图片。

标签: android xml android-layout android-relativelayout


【解决方案1】:

您需要使用dimens.xml根据设备管理边距

<RelativeLayout
            android:id="@+id/cellphone_screen"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/marginLeft"
            android:layout_marginRight="@dimen/marginRight"
            android:layout_marginTop="@dimen/marginTop"
            android:layout_marginBottom="@dimen/marginBottom"
            android:background="#e34579">

        </RelativeLayout>

【讨论】:

    【解决方案2】:

    尝试从主 RalativeLayout 中删除边距

            android:layout_marginLeft="@dimen/marginLeft"
            android:layout_marginRight="@dimen/marginRight"
            android:layout_marginTop="@dimen/marginTop"
            android:layout_marginBottom="@dimen/marginBottom"
    

    【讨论】:

      猜你喜欢
      • 2019-01-15
      • 1970-01-01
      • 2017-06-05
      • 2012-07-08
      • 1970-01-01
      • 2015-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多