我有一个RecyclerView
,上面还有一个AdView
。滚动RecyclerView
时,我想将Adview
保持在固定位置。我该怎么办?
这是我打开应用程序时的RecyclerView
。
滚动RecyclerView
我正在使用FrameLayout,方法是单击以下打开的片段菜单:
frg = (Fragment)
ContainerFragment.newInstance(getString(R.string.DefaultGaleria), getString(R.string.DefaultFrase), getString(R.string.DefaultGif));
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.container, frg);
transaction.commit();
这是布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<com.google.android.gms.ads.AdView
android:id="@+id/adViewMob"
android:background="@color/transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/ad_unit_id"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
我的activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:animateLayoutChanges="true"
>
<!-- android:layout_above="@+id/linear"-->
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coordinator"
>
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
android:fitsSystemWindows="true"
>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
app:tabMode="fixed"
/>
</com.google.android.material.appbar.AppBarLayout>
<!--<include layout="@layout/sample" />
<include layout="@layout/content_main" />
-->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_above="@+id/adViewMob"
android:animateLayoutChanges="true"
android:fitsSystemWindows="false"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/activity_main_drawer"
app:headerLayout="@layout/nav_header_main"
android:theme="@style/NavigationView"
app:itemTextColor="#ffffff"
android:background="#1a1b1d"
/>
<!--app:itemBackground="@drawable/good" -->
<!-- <android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />-->
</androidx.drawerlayout.widget.DrawerLayout>
答案 0 :(得分:0)
您可以使用RelativeLayout
轻松地完成此操作,如下所示。
<?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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="50dp" />
<com.google.android.gms.ads.AdView
android:id="@+id/adViewMob"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/transparent"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/ad_unit_id" />
</RelativeLayout>
这应该达到您的目的。
答案 1 :(得分:0)
将此代码放入您的xml文件中。
<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">
<com.google.android.gms.ads.AdView
android:id="@+id/adViewMob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/ad_unit_id" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_below="@+id/adViewMob"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>