为什么我的 adView 只占屏幕区域的一半,然后就没有了?

Why my adView is half in screen area and helf out?

我以编程方式将 adview 添加到我的应用中,我希望它 added/removed(如果用户进行应用内购买)低于我的回收站视图。我有意将 mu Recycler 视图和 adview 放在线性布局(容器)中,我在代码中引用它,adding/removing adview。但是,当我尝试它时,我的 adview 在屏幕上只显示了一半,并且在它的右侧有 space,如下所示:

这是我的 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
            xmlns:fab="http://schemas.android.com/apk/res-auto"
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:id="@+id/myMainRelativeLayout"
            android:layout_height="match_parent"
            tools:context=".MainActivity">
<include
    android:id="@+id/tool_bar"
    layout="@layout/tool_bar">
</include>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="100"
    android:layout_centerInParent="true"
    android:id="@+id/myAdContainer"
    android:layout_below="@+id/tool_bar">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:focusable="true"
        android:layout_weight="50"
        android:clickable="true"
        android:background="?android:selectableItemBackground"
        android:id="@+id/recyclerView"
    />
</LinearLayout>

<TextView android:id="@+id/list_empty"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="No Shopping Items yet"
          android:layout_centerVertical="true"
          android:layout_centerHorizontal="true"
    />

    <com.software.shell.fab.ActionButton
        android:id="@+id/buttonFloat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        fab:show_animation="@anim/fab_roll_from_down"
        fab:hide_animation="@anim/fab_roll_to_down"/>

这是添加广告视图的代码:

// does the user have the premium upgrade?
            mIsRemoveAdds = inventory.hasPurchase(SKU_REMOVE_ADDS);
            if(!mIsRemoveAdds) {
                //play ads
                mAdView = new AdView(MainActivity.this);
                mAdView.setAdSize(AdSize.BANNER);
                mAdView.setAdUnitId(getString(R.string.banner_ad_unit_id));
                AdRequest adRequest = new AdRequest.Builder().build();
                mAdView.loadAd(adRequest);

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                adContainer = (LinearLayout)findViewById(R.id.myAdContainer);
                //check if the adview is already added (count ==2) otherwise added
                int i = adContainer.getChildCount();
                if(! (i > 1)) {
                    adContainer.addView(mAdView, params);
                }
                //Toast.makeText(MainActivity.this,"no premium",Toast.LENGTH_LONG).show();
            }else{
                //remove ads
                if(mAdView != null) {
                    adContainer.removeView(mAdView);
                }
                //Toast.makeText(MainActivity.this,"premium",Toast.LENGTH_LONG).show();
            }
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="100"
    android:layout_centerInParent="true"
    android:id="@+id/myAdContainer"
    android:layout_below="@+id/tool_bar">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:focusable="true"
        android:layout_weight="50"
        android:clickable="true"
        android:background="?android:selectableItemBackground"
        android:id="@+id/recyclerView"
    />
</LinearLayout>

删除权重和weightSum。

试试这个

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="100"
    android:layout_centerInParent="true"
    android:id="@+id/myAdContainer"
    android:layout_below="@+id/tool_bar">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:focusable="true"
        android:layout_weight="100"
        android:clickable="true"
        android:background="?android:selectableItemBackground"
        android:id="@+id/recyclerView"
    />
</LinearLayout>

我在这里发现的问题是,您将 100 的权重分配给 ViewGroup [LinearLayout],而仅将其中的 50 分配给 View [RecyclerView]。所以上面利用整个分配的 viewGroup space 的解决方案应该有助于它完全显示它。