简体   繁体   中英

Resize layout when removing ads

I have in the MainActivity the typical activity_main with an adaptive banner. The user has the option to buy the app without ads and when there are no ads it is fine but when there is a banner I need to somehow resize the layout so that the Button and the RecyclerView are above the banner. Can you help me please. Thanks you in advance.

XML :

androidx.constraintlayout.widget.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="match_parent"
    android:animateLayoutChanges="true"
    android:background="@color/colorFondo"
    tools:context=".MainActivity">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorFondo"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/bannerContainer">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:listitem="@layout/paciente_item" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/boton_add_paciente"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="20dp"
            app:srcCompat="@drawable/ic_add_white_24dp" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <FrameLayout
        android:id="@+id/bannerContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">
    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

I have this: ihave

I want this: iwant

In the constraint layout bind the coordinate layout (button and recyclerview) to bannerContainer top and not to parent. When the banner exists they will be above the ad banner and when it doesn't in Java code set banner height to zero and it'll be fine.

I have redone the complete layout. I have changed the FloatingActionButton to an ImageButton and removed the CoordinatorLayout. This is how it turned out and it works:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="@color/colorFondo"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/bannerContainer"
    tools:listitem="@layout/paciente_item" >
</androidx.recyclerview.widget.RecyclerView>

<ImageButton
    android:id="@+id/boton_add_paciente"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:contentDescription="@string/app_name"
    app:layout_constraintBottom_toTopOf="@+id/bannerContainer"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    android:background="@drawable/ic_plus">
</ImageButton>

<FrameLayout
    android:id="@+id/bannerContainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">
</FrameLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM