Search code examples
androidcssxmlandroid-layoutscrollview

Useless Parent layout


** This ScrollView layout or its LinearLayout parent is useless; transfer the background attribute to the other view 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. **

`

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"

android:padding="10dp"
android:background="#504b4b"
tools:context="com.example.sompod.imageview.MainActivity">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible"
    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center"
        >

        <ImageView
            android:id="@+id/p1"
            android:layout_width="260dp"
            android:layout_height="260dp"

            android:layout_marginLeft="25dp"
            android:layout_marginStart="25dp"
            android:contentDescription="@null"
            android:rotation="270"


            android:scaleType="fitXY"
            android:src="@drawable/img_5116"
            />

        <ImageView
            android:id="@+id/p2"
            android:layout_width="300dp"
            android:layout_height="200dp"
            android:layout_marginLeft="15dp"
            android:layout_marginStart="15dp"
            android:layout_marginTop="25dp"
            android:contentDescription="@null"
            android:scaleType="fitXY"
            android:src="@drawable/bdboss" />

        <ImageView
            android:id="@+id/p3"
            android:layout_width="292dp"
            android:layout_height="335dp"
            android:layout_marginLeft="20dp"
            android:layout_marginStart="20dp"
            android:layout_marginTop="30dp"
            android:contentDescription="@null"
            android:rotation="270"
            android:scaleType="fitXY"
            android:src="@drawable/img_5123" />


        <ImageView
            android:id="@+id/p4"
            android:layout_width="225dp"
            android:layout_height="279dp"
            android:layout_marginLeft="25dp"
            android:layout_marginStart="25dp"
            android:layout_marginTop="15dp"
            android:contentDescription="@null"
            android:scaleType="fitXY"
            android:src="@drawable/bestone1" />


    </LinearLayout>

</ScrollView>


</LinearLayout>

`


Solution

  • Parent LinearLayout is useless - no value added. You can achive the same in this way in simpler way:

    <ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#504b4b"
    tools:context="com.example.sompod.imageview.MainActivity">
    
        <LinearLayout
            android:clipToPadding="false"
            android:padding="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center"
            >
    
            <ImageView
                android:id="@+id/p1"
                android:layout_width="260dp"
                android:layout_height="260dp"
    
                android:layout_marginLeft="25dp"
                android:layout_marginStart="25dp"
                android:contentDescription="@null"
                android:rotation="270"
    
    
                android:scaleType="fitXY"
                android:src="@drawable/img_5116"
                />
    
            <ImageView
                android:id="@+id/p2"
                android:layout_width="300dp"
                android:layout_height="200dp"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:layout_marginTop="25dp"
                android:contentDescription="@null"
                android:scaleType="fitXY"
                android:src="@drawable/bdboss" />
    
            <ImageView
                android:id="@+id/p3"
                android:layout_width="292dp"
                android:layout_height="335dp"
                android:layout_marginLeft="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginTop="30dp"
                android:contentDescription="@null"
                android:rotation="270"
                android:scaleType="fitXY"
                android:src="@drawable/img_5123" />
    
    
            <ImageView
                android:id="@+id/p4"
                android:layout_width="225dp"
                android:layout_height="279dp"
                android:layout_marginLeft="25dp"
                android:layout_marginStart="25dp"
                android:layout_marginTop="15dp"
                android:contentDescription="@null"
                android:scaleType="fitXY"
                android:src="@drawable/bestone1" />
    
        </LinearLayout>
    
      </ScrollView>