Android 使用CoordinatorLayout时,AdView和FloatingAction按钮重叠
在将新的Android 使用CoordinatorLayout时,AdView和FloatingAction按钮重叠,android,admob,floating-action-button,android-snackbar,android-coordinatorlayout,Android,Admob,Floating Action Button,Android Snackbar,Android Coordinatorlayout,在将新的坐标布局合并到我的一个布局中后,我发现Adviews与FloatingActionButton重叠的问题 <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main_
坐标布局
合并到我的一个布局中后,我发现Adview
s与FloatingActionButton
重叠的问题
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
<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/viewpager"
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/ic_done" />
</android.support.design.widget.CoordinatorLayout>
要复制的简单Admob示例:
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
((AdView)findViewById(R.id.ad_view)).loadAd(adRequest);
我有两个问题:
- 重叠视图(很可能是因为协调布局扩展了框架布局)
- 在与
floatingAction按钮相同的布局中添加其他视图会使其不再移动或与
SnackBar“协调”
我怎样才能正确地实现这一点呢?发布后,我决定尝试将整个
协调布局
包装在线性布局
中,它似乎工作得很好。也许有人知道更好的方法
我不确定这是最好的方式,因为有许多嵌套视图:
<LinearLayout 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:orientation="vertical"
tools:context=".activities.MainActivity">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
<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/viewpager"
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/ic_done" />
<com.google.android.gms.ads.AdView
android:id="@+id/ad_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
app:adSize="SMART_BANNER"
app:adUnitId="@string/app_ad" />
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
可能不是您想要的,但是您至少可以对齐
浮动操作按钮,就像这样
<com.google.android.gms.ads.AdView
android:id="@+id/ad_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
app:adSize="SMART_BANNER"
app:adUnitId="@string/app_ad" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_done"
app:layout_anchor="@id/adView"
app:layout_anchorGravity="top|end|right"
/>
为什么这是一个被否决的问题?看起来这是一个有效的问题,您的查询解决了我的其他问题。谢谢+1为您的question@Naruto非常感谢。这影响了我的一些应用程序,我想我会尝试询问社区。我不喜欢所有的“嵌套”布局。也许你可以使用FrameLayout而不是LinearLayout,它应该更轻量级。这个解决方案很好,但我与它有重叠:(
<com.google.android.gms.ads.AdView
android:id="@+id/ad_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
app:adSize="SMART_BANNER"
app:adUnitId="@string/app_ad" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_done"
app:layout_anchor="@id/adView"
app:layout_anchorGravity="top|end|right"
/>