我在我的应用中展示了admob广告,我想要做的是使用advert_container
的高度来设置marginBottom
的{{1}}。
这些是我的布局;
activity_book
book_frame
book_toolbar
<?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">
<include layout="@layout/book_toolbar"/>
<include layout="@layout/share_layout"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:visibility="gone"
android:background="#777777"
android:id="@+id/advert_container"
android:layout_centerHorizontal="true">
<View
android:layout_width="match_parent"
android:layout_height="15dp"
android:background="#777777"
android:id="@+id/dviewtop"/>
<com.google.android.gms.ads.AdView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/banner_id"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-XXXXXXXXXXXXXXXXXXXXXXXXX"/>
</LinearLayout>
</RelativeLayout>
这是我的活动:
BookActivity
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
tools:context=".BookActivity">
<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/book_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.AppBarLayout>
<FrameLayout
android:id="@+id/book_frame"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>
问题在于它抛出了//I skipped a lot a of codes that's not necessary
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate called");
setContentView(R.layout.activity_book);
mToolbar = (Toolbar) findViewById(R.id.book_toolbar);
mFragLayout = (FrameLayout) findViewById(R.id.book_frame);
advertCont = (LinearLayout) findViewById(R.id.advert_container);
adView = (AdView) findViewById(R.id.banner_id);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
.build();
adView.loadAd(adRequest);
ViewTreeObserver observer = advertCont.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
adHeight = adView.getMeasuredHeight();
Log.d(TAG, "The height of the advertL is " + adHeight);
if (adHeight > 1) {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)mFragLayout.getLayoutParams();
params.setMargins(0, 0, 0, adHeight);
mFragLayout.setLayoutParams(params);
}
}
});
}
异常。
堆栈跟踪
ClassCastException
在堆栈跟踪中,第87行是06-17 13:59:56.637 31869-31869/com.ozuf.bookhistory E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ozuf.bookhistory, PID: 31869
java.lang.ClassCastException: android.support.design.widget.CoordinatorLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
at com.ozuf.bookhistory.BookActivity$1.onGlobalLayout(BookActivity.java:87)
at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:912)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1911)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1073)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5988)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
。
请对此有解决方案吗?
答案 0 :(得分:1)
尝试更改
FrameLayout.LayoutParams params =
(FrameLayout.LayoutParams)mFragLayout.getLayoutParams();
到
CoordinatorLayout.LayoutParams params =
(CoordinatorLayout.LayoutParams)mFragLayout.getLayoutParams();
这是因为协调器布局是父布局,您需要设置父布局的布局参数。
答案 1 :(得分:1)
LayoutPrams适用于最顶层视图或当前视图的父级 ....
使用此
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams)mFragLayout.getLayoutParams();
而不是
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)mFragLayout.getLayoutParams();
享受编码...