【问题标题】:White statusbar when using Admob in activity layout在活动布局中使用 Admob 时的白色状态栏
【发布时间】:2018-07-30 21:20:34
【问题描述】:

我使用 CoordinatorLayout 和 RelativeLayout 在底部显示广告。但我得到的是白色状态栏。当我删除相对布局和 adview 一切正常。当我将 CoordinatorLayout 包装在 RelativeLayout 中时,我得到的是一个白色状态栏。

使用 API v23(棉花糖)

我也在这里找到了相关问题:Status bar turns white and does not show content behind it

我使用的布局看起来类似于这里的答案:https://stackoverflow.com/a/31065486/1820644

但没有帮助我。我仍然得到白色的状态栏。

我的 activity_layout.xml 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    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:fitsSystemWindows="true"
    tools:context="com.testapp.EventActivity">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/adView">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </android.support.design.widget.AppBarLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:layout_margin="@dimen/fab_margin"
            android:src="@drawable/btn_plus_add" />

    </android.support.design.widget.CoordinatorLayout>

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

</RelativeLayout>

提前致谢!

【问题讨论】:

    标签: android android-layout admob


    【解决方案1】:

    在尝试了几乎所有的布局、嵌套布局之后,最终奏效的是将 CoordinatorLayout 作为根,并在 adView 上方放置另一个 CoordinatorLayout。

    我使用的布局层次结构是这样的:

    <android.support.design.widget.CoordinatorLayout
    ...  >
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <android.support.design.widget.CoordinatorLayout
                ....
                android:layout_above="@+id/adView">
    
                <android.support.v4.view.ViewPager
                    ... />
    
            </android.support.design.widget.CoordinatorLayout>
    
            <com.google.android.gms.ads.AdView
                android:id="@+id/adView"
                ... />
    
        </RelativeLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    

    因此,作为根的 CoordinatorLayout,它确保了状态栏的颜色。

    我试图通过嵌套 CoordinatorLayoutCoordinatorLayout 作为活动布局的根元素来实现。

    我的布局文件的完整代码:

    <android.support.design.widget.CoordinatorLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        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:fitsSystemWindows="true"
        tools:context="com.testapp.EventActivity">
    
        <RelativeLayout
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <android.support.design.widget.CoordinatorLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@+id/adView">
    
                <android.support.design.widget.AppBarLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:theme="@style/AppTheme.AppBarOverlay">
    
                    <android.support.v7.widget.Toolbar
                        android:id="@+id/toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        android:background="?attr/colorPrimary"
                        app:layout_scrollFlags="scroll|enterAlways"
                        app:popupTheme="@style/AppTheme.PopupOverlay" />
    
                    <android.support.design.widget.TabLayout
                        android:id="@+id/tabs"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
    
                </android.support.design.widget.AppBarLayout>
    
                <android.support.v4.view.ViewPager
                    android:id="@+id/container"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/fab"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="end|bottom"
                    android:layout_margin="@dimen/fab_margin"
                    android:src="@drawable/btn_plus_add" />
    
            </android.support.design.widget.CoordinatorLayout>
    
            <com.google.android.gms.ads.AdView
                android:id="@+id/adView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                ads:adSize="SMART_BANNER"
                ads:adUnitId="@string/banner_ad_bottom_event" />
    
        </RelativeLayout>
    </android.support.design.widget.CoordinatorLayout>
    

    终于变成了这个样子:

    【讨论】:

    • 它甚至可以使用单个协调器布局并且根本没有相对布局。
    猜你喜欢
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 2021-09-25
    相关资源
    最近更新 更多