假的缺少布局属性

Fake missing layout attributes

我在 LinearLayout 中有一个分隔视图,如下所示:

<View style="@style/Separator" />

Android Studio 4.1.3 Lint(通过分析>检查代码...)告诉我这一行:

The required layout_width and layout_height attributes are missing

然而,style/Separator 包含在我使用的库中,通过库文件 res/values/style.xml 其内容是:

....
<style name="Separator">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">1dp</item>
    <item name="android:layout_marginBottom">0dp</item>
    <item name="android:layout_marginLeft">3dp</item>
    <item name="android:layout_marginRight">3dp</item>
    <item name="android:layout_marginTop">0dp</item>
    <item name="android:background">#007788</item>
</style>

如您所见,layout_widthlayout_height 就在那里,并且实际上被 Android 阅读和尊重。

这是怎么回事,我该如何解决这个烦人的 lint(显然无需更改检查设置或抑制 lint)?

编辑: 只有当我使用库中的 View 时才会发生这种情况。因此,如果样式文件是应用程序的本地文件,则不会发出 lint 错误。

您需要将View中的layout_widthlayout_height设置为参数。 原因是: 布局值在您的 style.xml 中无法访问,但在您的 Layout 中(顾名思义。)

在您的 Layout 中应该是这样的:

<View style="@style/Separator"
 android:layout_width="fill_parent"
 android:layout_height="w1dp" />

在你的 styles.xml:

....
<style name="Separator">
    <item name="android:layout_marginBottom">0dp</item>
    <item name="android:layout_marginLeft">3dp</item>
    <item name="android:layout_marginRight">3dp</item>
    <item name="android:layout_marginTop">0dp</item>
    <item name="android:background">#007788</item>
</style>