我在我的Android应用中使用com.google.android.gms.ads.AdView在我的Xml文件中创建了一个google广告,如下所示。过了一会儿,我注意到广告向左走,并保持正确的空间。我将引力更改为“center”,将layout_gravity更改为居中但仍然相同 这是我的xml
<LinearLayout
android:id="@+id/linear"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@color/White"
android:orientation="vertical" >
<include
android:id="@+id/main_bar"
layout="@layout/top_main_action_bar" />
<RelativeLayout
android:id="@+id/relative"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.9" >
<include layout="@layout/channel_list_viewpager" />
<ProgressBar
android:id="@+id/progressBar"
style="@android:style/Widget.Holo.Light.ProgressBar.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="gone" />
<ListView
android:id="@+id/listOverflow"
android:layout_width="@dimen/overflow_width"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/popup_menu_shadow"
android:cacheColorHint="@android:color/transparent"
android:divider="@null"
android:dividerHeight="0dp"
android:listSelector="#00000000"
android:visibility="gone" />
</RelativeLayout>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
ads:adSize="SMART_BANNER"
ads:adUnitId="/7229/Assafir_Mobile_Apps" />
</LinearLayout>
答案 0 :(得分:0)
Gravity center will not work as you adview width and height is wrap content.
LayoutGravity "center" will only work if parent layout is Linear Layout.
You have to user horizontal center true if you parent layout is a Relative Layout and make sure that adView's parent layout is not wrap content.
You can do something like this -
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<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="/722569/ID" />
</RelativeLayout>
Or
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="/722569/ID" />
</RelativeLayout>
答案 1 :(得分:0)
试试这个,
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="/7229/Assafir_Mobile_Apps" />