alignParentBottom 在具有滚动视图的片段中出现在屏幕外

alignParentBottom appears Offscreen in Fragment with Scroll View

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

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

解决方案是将 adView 移动到用于我的片段的 Coordinator 布局之外的 Main Activity 布局中。然后我将 Coordinator Layout 和 adView 包装在一个 Relative Layout 中。

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

我发现当我更改 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>