【问题标题】:LinearLayout - Lint error when using style file (still working, though)LinearLayout - 使用样式文件时出现 Lint 错误(但仍在工作)
【发布时间】:2018-02-12 09:57:16
【问题描述】:

问题:当在样式文件中定义 LinearLayout 的方向时,我收到一个 lint 错误和警告,但在直接在元素上定义方向时却没有。即使该属性是从样式文件中提取的。

我的所有活动都有一个基本样式,包含以下内容:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="StandardActivity">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:orientation">vertical</item>
    </style>
</resources>

在我的布局中,如果我添加此代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/StandardActivity">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

    ...

    </LinearLayout>

    ...

</LinearLayout>

layout_width 上,我收到一个 lint 警告:

Use a 'layout_width' of '0dp' instead of 'fill_parent' for better performance

layout_height 上,我收到一个 lint 错误:

Suspicious size: this will make the view invisible, probably intended for 'layout_width'

一切都按预期工作,布局采用StandardActivity 中设置的orientation 属性。但是,错误和警告在方向设置为horizontal 时有效。如何让 Android Studio 中的 lint 了解样式文件中设置的方向?

如果直接将方向显式添加到 LinearLayout,则 linting 错误和警告会消失。

【问题讨论】:

    标签: android android-studio android-linearlayout android-lint


    【解决方案1】:

    没有问题,IDE 可能落后了。它的假设是layout_orientationhorizontal,在这种情况下这些错误是正确的。但是,正如您在style 中创建的layout_orientation=vertical 一样,一切都很好。 如果您要将这些变量直接转移到布局中,这些错误应该会消失。 PS fill_parent 已弃用,请使用 match_parent

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"   >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical">
    
        ...
    
        </LinearLayout>
    
        ...
    
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-27
      • 2021-09-22
      • 2020-01-01
      相关资源
      最近更新 更多