【问题标题】:alignParentBottom appears Offscreen in Fragment with Scroll ViewalignParentBottom 在带有滚动视图的片段中出现在屏幕外
【发布时间】:2017-01-04 23:33:30
【问题描述】:

对于那些希望在他们的应用中包含由 Fragment 布局组成的 adView 的人,希望以下内容对您有所帮助!

基本上,我最初试图将 adView 放置在每个片段的布局 XML 中。这导致 adView 要么被推离屏幕,要么不能很好地使用相关布局命令(例如 alignParentBottom)。

解决方案是将 adView 移到用于我的片段的协调器布局之外的主活动布局中。然后我将 Coordinator Layout 和 adView 包装在一个相对布局中。

这让我可以完全控制 adView 并显示在固定到屏幕底部的每个片段上。

【问题讨论】:

  • 我还没有测试过,也不确定它是否能满足您的需求,但是如果您将alignParentBottom 用于您的广告视图并将广告视图上方的ScrollViewandroid:layout_above 对齐会怎样?
  • 当我尝试你的建议时,奇怪的是这个。错误:(98, 31) 未找到与给定名称匹配的资源(位于“layout_above”,值为“@id/adView”)。
  • 我认为您的问题是 AdView 在代码中的 ScrollView 之前。 ScrollView 可能会覆盖 AdView。尝试将 AdView 放在 ScrollView 下方。另外,请确保您的广告单元 ID 和所有内容均正确无误。

标签: android user-interface alignment android-relativelayout


【解决方案1】:

我发现当我更改 ScrollView 的背景时,它会覆盖 AdView。您必须将 AdView 放在代码中的 ScrollView 下方。这样,它在 ScrollView 之后绘制,因此位于顶部。

这是您编辑的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp">

    <!-- Dummy item to prevent EditTextView from receiving focus -->
    <LinearLayout
        android:id="@+id/dummyLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:orientation="horizontal" />
    <!-- Dummy item to prevent EditTextView from receiving focus -->

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="4"
            android:hint="@string/hint"
            android:nextFocusLeft="@id/editText1"
            android:nextFocusUp="@id/editText1"
            android:singleLine="true"/>

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/linearLayout1"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView4"/>

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/button1"
        android:background="@color/colorAccent0"
        android:stretchColumns="*"/>

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tableLayout1"
        android:layout_above="@+id/adView"
        android:background="@color/colorGray">

        <TableLayout
            android:id="@+id/tableLayout2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:stretchColumns="*"/>
    </ScrollView>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>

</RelativeLayout>

【讨论】:

  • 谢谢!在阅读了一个更老的 SO 线程后,我将 adview 扔到了滚动视图上方。应该早点尝试逆转哈。
  • 抱歉,我被伪造了,滚动视图下方的移动广告视图仍未出现在屏幕上
  • 这很奇怪,因为我用上面粘贴的代码让它显示在我的设备上。也许也可以试试android:layout_below="@+id/scrollView"
  • 看起来有帮助。它现在在屏幕上和滚动视图下方,但未固定在屏幕底部(随着滚动视图向上/向下移动)。也许是因为布局是一个片段?
  • 尝试将 ScrollView 设置为 android:layout_height="wrap_content",将 ScrollView 设置为 android:layout_above="@+id/adView" 并删除 android:layout_below="@+id/scrollView"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多