首页 > 解决方案 > 如何修复在 Android 中提供 layout_width 属性

问题描述

在我的应用程序中,我想使用并为此RelativeLayout设置大小dimens
我编写我的 XML 代码,如下所示:

            <RelativeLayout
                android:id="@+id/listItemAuction_productImgLay"
                android:layout_width="@dimen/size120New"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginBottom="@dimen/padding5"
                android:layout_marginRight="@dimen/padding5"
                android:layout_marginTop="5dp">
            </RelativeLayout>

尺寸: <dimen name="size120New">120</dimen>

但是当运行应用程序时显示以下错误Logcat

java.lang.RuntimeException: Binary XML file line #26: You must supply a layout_width attribute.

我该如何解决它和我们dimes

标签: javaandroidandroid-layout

解决方案


您的宽度尺寸需要一个单位

120 本身是没有意义的。您可能想要 120dp。dp 是物理测量单位,与分辨率无关,这就是 android 与不同设备保持兼容性的方式。

例如:

<dimen name="size120New">120dp</dimen>

还有其他单位,这个答案解释得很好: https ://stackoverflow.com/a/2025541/2762277


推荐阅读