Andrea Cola
Andrea Cola

Reputation: 127

Reusing layouts with ConstraintLayout

I want to reuse my basic layout components such as buttons and textview. The file main.xml has a ConstraintLayout and when I include the layout of the customized TextView the main layout completely ignore layout_margins fixed in the custom_textview.xml. There is a particular way to include/merge a layout inside a ConstraintLayout.

Main layout:

<?xml version="1.0" encoding="utf-8"?>
<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_height="match_parent"
    android:layout_width="match_parent"
    android:fillViewport="true" >

    <android.support.constraint.ConstraintLayout 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="wrap_content">

        <ImageView
            android:id="@+id/profile_image"
            android:layout_width="150dp"
            android:layout_height="0dp"
            android:layout_marginStart="@dimen/default_margin"
            android:layout_marginTop="@dimen/default_top_bottom_parent_margin"
            android:layout_marginEnd="@dimen/default_margin"
            app:layout_constraintDimensionRatio="h,1:1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@mipmap/ic_launcher_round"
            tools:srcCompat="@tools:sample/avatars" />

        <include
            android:id="@+id/name_surname"
            layout="@layout/standard_textview_layout"
            android:layout_height="56dp"
            android:layout_width="match_parent"/>

    </android.support.constraint.ConstraintLayout>

</ScrollView>

This is the layout of the custom TextView:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/standard_textview"
    android:layout_width="0dp"
    android:layout_height="@dimen/default_height"
    android:textSize="@dimen/default_text_size"
    android:layout_margin="@dimen/default_margin"
    android:layout_marginBottom="0dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf=""
    app:layout_constraintWidth_max="@dimen/default_max_width"
    app:layout_constraintWidth_min="@dimen/default_min_width" />

Upvotes: 0

Views: 1198

Answers (1)

TheLibrarian
TheLibrarian

Reputation: 1888

If you include layout all "Inflate params"(width,height,margin,padding) are ignored.

Change root element in the include file to merge and have the TextView there.

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

    <TextView
        android:id="@+id/standard_textview"
        android:layout_width="0dp"
        android:layout_height="@dimen/default_height"
        ... />
</merge>

Upvotes: 1

Related Questions