Search code examples
androidandroid-linearlayoutandroid-constraintlayout

Out of curiosity, why are ConstraintLayout and LinearLayout declared inconsistently?


In the Android Documentation, you are required to write android.support.constraint.ConstraintLayout to declare a ConstraintLayout. To declare a LinearLayout, all you need is LinearLayout. Why isn't this consistent?

For example: (Taken directly from from Android Documentation)

ConstraintLayout

<android.support.constraint.ConstraintLayout ...>
             <Button android:id="@+id/button" ...
                 app:layout_constraintLeft_toLeftOf="parent"
                 app:layout_constraintRight_toRightOf="parent/>
         </>

LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingLeft="16dp"
   android:paddingRight="16dp"
   android:orientation="horizontal"
   android:gravity="center">

   <!-- Include other widget or layout tags here. These are considered
           "child views" or "children" of the linear layout -->

 </LinearLayout>

Why can't you just write <ConstraintLayout ... other than That's Just the Way it Is?


Solution

  • Why isn't this consistent?

    LinearLayout is a framework class, in android.widget. The framework LayoutInflater knows to look in certain packages, such as android.widget, for bare class names like LinearLayout.

    ConstraintLayout is from a library. The framework LayoutInflater does not know anything about this library, and it will not find ConstraintLayout in android.widget or other framework packages.

    For library-contributed classes, we need to fully-qualify the class name in the XML element.