Вопрос

I have some layout file which Lint is giving me the following warning about:

UselessParent

Summary: Checks whether a parent layout can be removed.

Priority: 2 / 10 Severity: Warning Category: Performance

A layout with children that has no siblings, is not a scrollview or a root layout, and does not have a background, can be removed and have its children moved directly into the parent for a flatter and more efficient layout hierarchy.

The part I'm confused about is in bold. My layout (before fixing this warning) is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/default_margin"
        android:paddingTop="@dimen/default_margin_small"
        android:paddingBottom="@dimen/default_margin_small">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingRight="@dimen/default_margin"
            android:orientation="vertical">

            <!-- TEXTVIEWS & EDITTEXTS HERE -->

        </LinearLayout>

    </ScrollView>

</LinearLayout>

But I fixed the warning like this:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/default_margin"
    android:paddingTop="@dimen/default_margin_small"
    android:paddingBottom="@dimen/default_margin_small">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingRight="@dimen/default_margin"
        android:orientation="vertical">

        <!-- TEXTVIEWS & EDITTEXTS HERE -->

    </LinearLayout>

</ScrollView>

This makes sense to me but the bold part above in the Lint check is confusing me a bit, because I do have a ScrollView inside a LinearLayout. Should I not be fixing it like this or is there no problem having a ScrollView as root element for a layout file since a ScrollView inherits from FrameLayout anyway?

Это было полезно?

Решение

There is no problem having a ScrollView as root element for a layout file. The lint complaint presumably was about your original root LinearLayout, which was not adding any value with just the ScrollView as a child.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top