AdView 移动到替换的 Fragment 上方
AdView moves above replaced Fragment
这是一个相当简单的布局,基本上是一个 Google 地图,其下方有一个 AdView。当我替换整个布局(使用 FragmentManager)时,AdView 仍然可见并移动到屏幕顶部。为什么 AdView 仍然可见?是不是应该像MapFragment一样被替换掉?
activity_main.xml
<?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"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/banner_ad_unit_id" />
</LinearLayout>
MainActivity.java
getFragmentManager().beginTransaction()
.replace(R.id.container, new SettingsFragment())
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null)
.commit();
AdView 在底部
AdView 在顶部
这基本上是因为 FragmentManager 有点奇怪。使用片段时要记住的一点是:
Fragments created via XML layout should never be replaced.
如果您需要替换 activity 中的片段,您应该使用 Java 中的全部。像这样:
@Override
public void onCreate(Bundle state){
super.onCreate(state);
setContentView(R.layout...);
if(state == null){
getFragmentManager()... add initial fragment
}
}
private void moveToNextFrag(){
getFragmentManager()
.beginTransaction()
.replace(R.id.container, new SettingsFragment())
.... etc...etc
}
我知道这很奇怪,但这就是 FragmentManager
的运作方式。
这是一个相当简单的布局,基本上是一个 Google 地图,其下方有一个 AdView。当我替换整个布局(使用 FragmentManager)时,AdView 仍然可见并移动到屏幕顶部。为什么 AdView 仍然可见?是不是应该像MapFragment一样被替换掉?
activity_main.xml
<?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"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/banner_ad_unit_id" />
</LinearLayout>
MainActivity.java
getFragmentManager().beginTransaction()
.replace(R.id.container, new SettingsFragment())
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null)
.commit();
AdView 在底部
AdView 在顶部
这基本上是因为 FragmentManager 有点奇怪。使用片段时要记住的一点是:
Fragments created via XML layout should never be replaced.
如果您需要替换 activity 中的片段,您应该使用 Java 中的全部。像这样:
@Override
public void onCreate(Bundle state){
super.onCreate(state);
setContentView(R.layout...);
if(state == null){
getFragmentManager()... add initial fragment
}
}
private void moveToNextFrag(){
getFragmentManager()
.beginTransaction()
.replace(R.id.container, new SettingsFragment())
.... etc...etc
}
我知道这很奇怪,但这就是 FragmentManager
的运作方式。