【问题标题】:How to avoid ad occupying space of view directed by ConstraintLayout guideline in aspect of other view's space directed by other guideline?如何避免广告占用 ConstraintLayout 指导方针指导的视图空间,而其他视图的空间由其他指导方针指导?
【发布时间】:2021-04-20 10:53:54
【问题描述】:

当底部的广告出现时,它会减少 TextView1 的空间,但绿色 (#4CAF50) TextView2 的空间保持不变。即使在广告出现后,我也希望两个文本视图的空间百分比相同。我的意思是,在横幅出现后,指导方针应该会有所提升。我什至尝试通过将app:layout_constraintBottom_toTopOf="@+id/adView"添加到指南中来使编辑器从顶部计数到横幅,但它仍然从ConstraintLayout的顶部到底部计算百分比。我怎样才能防止这种情况发生?

<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"
    tools:context=".MainActivity">


    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        app:adSize="BANNER"
        app:layout_constraintBottom_toBottomOf="parent"
        app:adUnitId="ca-app-pub-3940256099942544/6300978111"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/topGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toTopOf="@+id/adView"
        app:layout_constraintGuide_percent="0.67"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/TextView1"

        android:layout_width="0dp"
        android:layout_height="0dp"

        android:background="@color/black"
        app:layout_constraintBottom_toTopOf="@+id/adView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/bottomGuideline" />

    <TextView
        android:id="@+id/TextView2"

        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#4CAF50"
        app:layout_constraintBottom_toTopOf="@+id/bottomGuideline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/topGuideline" />


    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/bottomGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toTopOf="@+id/adView"
        app:layout_constraintGuide_percent="0.85"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

编辑:我的主要目标是用piano keyboard 解决这个问题。

【问题讨论】:

    标签: android xml ads android-constraintlayout constraintlayout-guideline


    【解决方案1】:

    问题是android:layout_height="0dp"在使用Guidelines时引起的:这样当banner可见时黑色的TextView永远不会出现。

    始终采用黑色文本视图:app:layout_constraintTop_toTopOf="@id/bottomGuideline"app:layout_constraintBottom_toTopOf="@+id/adView":您是说将顶部设置为指南的顶部,它永远不会改变其位置,而底部则设置为横幅的底部。另外,您将 0dp 设置为高度,因此如果横幅的可见性消失,则黑色 textView 的底部将位于父级(屏幕)的底部。相反,如果横幅是可见的,则文本视图将获得横幅和指南之间的可用空间。

    你应该给wrap_content而不是0dp,但你应该考虑别的东西而不是指导方针。如果 TextView 内容太低而无法达到您想要的效果,您可以随时使用此属性android:minHeight="your_value_in_dp" 设置 TextView 的最小高度。

    【讨论】:

    • 非常感谢您向我解释。也许你可以就我的情况提出一些建议?是钢琴键盘。 imgur.com/a/qgyevMX 如您所见,当我将其设置为 wrap_content 时,我的密钥会缩小@
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    相关资源
    最近更新 更多