简体   繁体   English

移除广告时调整布局大小

[英]Resize layout when removing ads

I have in the MainActivity the typical activity_main with an adaptive banner.我在 MainActivity 中有一个带有自适应横幅的典型 activity_main。 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.用户可以选择购买没有广告的应用程序,当没有广告时它很好,但是当有横幅时,我需要以某种方式调整布局大小,以便 Button 和 RecyclerView 位于横幅上方。 Can you help me please.你能帮我吗。 Thanks you in advance.提前谢谢你。

XML : 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.在约束布局中,将坐标布局(按钮和recyclerview)绑定到bannerContainer 顶部而不是父级。 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.当横幅存在时,它们将在广告横幅上方,而当它不在 Java 代码中时,将横幅高度设置为零,这样就可以了。

I have redone the complete layout.我已经重做了完整的布局。 I have changed the FloatingActionButton to an ImageButton and removed the CoordinatorLayout.我已将 FloatingActionButton 更改为 ImageButton 并删除了 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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