【问题标题】:Why is Android lint showing "nested weights are bad for performance" when none are nested?为什么没有嵌套时,Android lint 会显示“嵌套权重对性能不利”?
【发布时间】:2012-04-22 21:08:41
【问题描述】:

此布局中最后一个 TextView 中的 layout_weight 属性被 lint 高亮显示,导致性能不佳。我怀疑 lint 警告的名称是错误的,因为没有嵌套权重。有几个拉伸视图,我怀疑这意味着依赖加权视图。

构建此布局并避免此 lint 警告并提高性能的解决方案是什么?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:gravity="center_horizontal"
          android:background="@drawable/background_gradient"
    >
<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image4"
        android:layout_margin="6dp"
        android:contentDescription="@string/PLACEHOLDER_TEXT"
        />
<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        >
    <Button
            style="@style/dashboard_button"
            android:id="@+id/home_management"
            android:text="@string/ICON_MANAGEMENT"
            />
    <Button
            style="@style/dashboard_button"
            android:id="@+id/home_calculators"
            android:text="@string/ICON_CALCULATORS"
            />
</LinearLayout>
<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        >
    <Button
            style="@style/dashboard_button"
            android:id="@+id/home_guidelines"
            android:text="@string/ICON_GUIDELINES"
            />
    <Button
            style="@style/dashboard_button"
            android:id="@+id/home_faq"
            android:text="@string/ICON_FAQ"
            />
</LinearLayout>
<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        >
    <Button
            style="@style/dashboard_button"
            android:id="@+id/home_refs"
            android:text="@string/ICON_REFERENCES"
            />
    <Button
            style="@style/dashboard_button"
            android:text=""
            android:visibility="invisible"
            />
</LinearLayout>
<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:layout_margin="6dp"
        >
    <TextView
            android:text="@string/HOMEPAGE_CONTENT"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:textStyle="bold"
            android:textColor="#FFFFFF"
            />
    <View
            android:layout_width="8dp"
            android:layout_height="1dp"
            />
    <ImageView
            android:id="@+id/home_logo"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/image7"
            android:contentDescription="@string/PLACEHOLDER_TEXT"
            />
</LinearLayout>
<Button
        android:id="@+id/home_web_site"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/HOMEPAGE_FOOTER"
        android:paddingLeft="4dp"
        android:paddingRight="4dp"
        android:paddingTop="8dp"
        android:paddingBottom="8dp"
        android:textSize="12sp"
        android:background="@drawable/button_red"
        android:layout_margin="6dp"
        android:textColor="#FFFFFF"
        />
</LinearLayout>

【问题讨论】:

标签: android android-lint


【解决方案1】:

不过,您不必担心这一点,尤其是当您确实需要权重时,并且这是正确声明布局的唯一方法。

【讨论】:

  • 如果我们在构建过程中允许警告,很难知道哪些警告是“好的”,哪些不是。我更喜欢零容忍的方法(通过修复警告或关闭警告),然后如果出现警告,则表示需要修复。
【解决方案2】:

@string/HOMEPAGE_CONTENT 区域中的 TextViewImageView 嵌套在它们的父级 LinearLayout 中并分配了权重。我怀疑这些是你的问题,特别是因为父元素是wrap_content,所以理论上不应该有任何空间可以让子元素伸展。

【讨论】:

  • 但是不是所有的加权视图都嵌套在某个东西里面吗?我对警告的解读是,它说另一个加权视图中的一个加权视图不好,但在我的场景中,没有加权视图相互嵌套。其中有四个,有些将依赖于其他。我仍然倾向于认为警告文本不正确,应该说“依赖”而不是“嵌套”。
  • @OllieC 是的,加权视图总是在某些东西的内部,但它并不总是有用。如果父视图高 1000 像素,但子视图仅占用 800 像素,则权重决定了如何分配额外空间。我怀疑这里的警告是父视图设置为完全占用其子视图(wrap_content)定义的空间量,因此权重不仅无用,而且可能会在布局期间触发不必要的昂贵View 测量。我远非专家,但对我来说似乎合乎逻辑。
  • 您只能很少知道有多少像素可用。这似乎不是那些时代之一。
  • @AlbeyAmakiir 嗯,是的。这就是我的观点。不过,用一些硬数字来说明这个概念会更容易。
【解决方案3】:

关于这部分代码。

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="horizontal"

<Button
        style="@style/dashboard_button"
        android:id="@+id/home_management"
        android:text="@string/ICON_MANAGEMENT"
        />
<Button
        style="@style/dashboard_button"
        android:id="@+id/home_calculators"
        android:text="@string/ICON_CALCULATORS"
        />

您的 LinearLayout 包含“layout_weight”属性,并且如果样式“dashboard_button”也包含属性“layout_weight”,那么您会得到几个带有“layout_weight”属性的嵌套视图,尽管 lint 现在没有警告我有关样式中的“layout_weight”。 我的解决方案是用 RelativLayout 替换一个(或两个)LinearLayouts,并以编程方式调整大小和位置。

【讨论】:

    猜你喜欢
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-18
    • 2013-01-21
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多