在Android的LinearLayout底部设置Adview Banner

时间:2015-02-03 06:35:07

标签: android android-linearlayout adview

嗨,以下是我的代码, 我希望AdView显示在屏幕的底部,但在我的代码中,它出现在最后一个控件的下方, 我将重力设置为底部以及布局重力作为底部事件,尽管我没有运气,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/playscreenbg"
tools:context="com.exp.exp.ExpActivity"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/ll_utilities"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="right"
    android:gravity="right">

    <Button
        android:id="@+id/btn_highscore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_highscore_selector"
        android:onClick="onClickLevels"
        android:enabled="false" />
    <Button
        android:id="@+id/btn_sound"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_sound_off_selector"
        android:onClick="onClickLevels"
        android:enabled="false" />

</LinearLayout>

<ImageView
    android:id="@+id/img_Logo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:layout_marginLeft="40dp"
    android:layout_marginRight="40dp"
    android:src="@drawable/logo" />

<LinearLayout
    android:id="@+id/ll_level_buttons"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
        android:id="@+id/main_easy_button"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:background="@drawable/btn_easy_selector"
        android:onClick="onClickLevels"
        android:shadowRadius="3.0"
        android:textColor="#00e589"
        android:textSize="20sp"
        android:textStyle="bold"
        android:enabled="false" />

    <Button
        android:id="@+id/main_normal_button"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/btn_normal_selector"
        android:onClick="onClickLevels"
        android:shadowRadius="2.0"
        android:textColor="#00e589"
        android:textSize="20sp"
        android:textStyle="bold"
        android:enabled="false" />

    <Button
        android:id="@+id/main_hard_button"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/btn_hard_selector"
        android:onClick="onClickLevels"
        android:shadowRadius="2.0"
        android:textColor="#00e589"
        android:textSize="20sp"
        android:textStyle="bold"
        android:enabled="false" />
</LinearLayout>

<com.google.android.gms.ads.AdView 
      xmlns:ads="http://schemas.android.com/apk/res-auto"
      android:id="@+id/adViewLandingPage"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      ads:adSize="BANNER"
      android:gravity="bottom"
      android:layout_gravity="bottom"
      />

请帮帮我。

6 个答案:

答案 0 :(得分:1)

只需在<com.google.android.gms.ads.AdView />视图上方添加此空白视图。

<View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

答案 1 :(得分:1)

你应该使用weight和weightSum属性进行线性布局。

试试这个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/ic_launcher"
    android:orientation="vertical"
    tools:context="com.exp.exp.ExpActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:orientation="vertical"
        android:weightSum="10" >

        <LinearLayout
            android:id="@+id/ll_utilities"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="right"
            android:layout_weight="1"
            android:gravity="right"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btn_highscore"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher"
                android:enabled="false"
                android:onClick="onClickLevels" />

            <Button
                android:id="@+id/btn_sound"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher"
                android:enabled="false"
                android:onClick="onClickLevels" />
        </LinearLayout>

        <ImageView
            android:id="@+id/img_Logo"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp"
            android:layout_marginTop="30dp"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <LinearLayout
            android:id="@+id/ll_level_buttons"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="7"
            android:orientation="vertical" >

            <Button
                android:id="@+id/main_easy_button"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="50dp"
                android:background="@drawable/ic_launcher"
                android:enabled="false"
                android:onClick="onClickLevels"
                android:shadowRadius="3.0"
                android:textColor="#00e589"
                android:textSize="20sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/main_normal_button"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="50dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/ic_launcher"
                android:enabled="false"
                android:onClick="onClickLevels"
                android:shadowRadius="2.0"
                android:textColor="#00e589"
                android:textSize="20sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/main_hard_button"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="50dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/ic_launcher"
                android:enabled="false"
                android:onClick="onClickLevels"
                android:shadowRadius="2.0"
                android:textColor="#00e589"
                android:textSize="20sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <com.google.android.gms.ads.AdView
                xmlns:ads="http://schemas.android.com/apk/res-auto"
                android:id="@+id/adViewLandingPage"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_gravity="bottom"
                ads:adSize="BANNER"
                android:gravity="bottom" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

答案 2 :(得分:0)

you have to use one more linear layout for ads


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/playscreenbg"
tools:context="com.exp.exp.ExpActivity"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/ll_utilities"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="right"
    android:gravity="right">

    <Button
        android:id="@+id/btn_highscore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_highscore_selector"
        android:onClick="onClickLevels"
        android:enabled="false" />
    <Button
        android:id="@+id/btn_sound"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_sound_off_selector"
        android:onClick="onClickLevels"
        android:enabled="false" />

</LinearLayout>

<ImageView
    android:id="@+id/img_Logo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:layout_marginLeft="40dp"
    android:layout_marginRight="40dp"
    android:src="@drawable/logo" />

<LinearLayout
    android:id="@+id/ll_level_buttons"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
        android:id="@+id/main_easy_button"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:background="@drawable/btn_easy_selector"
        android:onClick="onClickLevels"
        android:shadowRadius="3.0"
        android:textColor="#00e589"
        android:textSize="20sp"
        android:textStyle="bold"
        android:enabled="false" />

    <Button
        android:id="@+id/main_normal_button"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/btn_normal_selector"
        android:onClick="onClickLevels"
        android:shadowRadius="2.0"
        android:textColor="#00e589"
        android:textSize="20sp"
        android:textStyle="bold"
        android:enabled="false" />

    <Button
        android:id="@+id/main_hard_button"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/btn_hard_selector"
        android:onClick="onClickLevels"
        android:shadowRadius="2.0"
        android:textColor="#00e589"
        android:textSize="20sp"
        android:textStyle="bold"
        android:enabled="false" />
</LinearLayout>
<LinearLayout
    android:id="@+id/ll_utilities"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
<com.google.android.gms.ads.AdView 
      xmlns:ads="http://schemas.android.com/apk/res-auto"
      android:id="@+id/adViewLandingPage"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      ads:adSize="BANNER"
      android:gravity="bottom"
      android:layout_gravity="bottom"
      />

</LinearLayout>
</LinearLayout>

答案 3 :(得分:0)

为什么不将父级布局保留为RelativeLayout,将adView保留在布局底部:android:layout_alignParentBottom="true"

答案 4 :(得分:0)

正如@Adrian所建议的那样使用线性布局的权重属性。我的代码片段包含一个相对父容器,因为使用嵌套线性布局设置窗口小部件的方式肯定会导致过度绘制问题以及渲染性能不佳。相反使用相对布局并获得所需的结果。嵌套权重对性能也有害。只是说。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                xmlns:ads="http://schemas.android.com/apk/res-auto"
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/playscreenbg"
                tools:context="com.exp.exp.ExpActivity"
                android:orientation="vertical">

    <Button
        android:id="@+id/btn_highscore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_highscore_selector"
        android:onClick="onClickLevels"
        android:enabled="false"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/btn_sound"
        android:layout_toStartOf="@+id/btn_sound"/>

    <Button
        android:id="@+id/btn_sound"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_sound_off_selector"
        android:onClick="onClickLevels"
        android:enabled="false"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

    <ImageView
        android:id="@+id/img_Logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo"
        android:layout_below="@+id/btn_highscore"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <LinearLayout
        android:id="@+id/ll_level_buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_above="@+id/adViewLandingPage"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <Button
            android:id="@+id/main_easy_button"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:background="@drawable/btn_easy_selector"
            android:onClick="onClickLevels"
            android:shadowRadius="3.0"
            android:textColor="#00e589"
            android:textSize="20sp"
            android:textStyle="bold"
            android:enabled="false"/>

        <Button
            android:id="@+id/main_normal_button"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/btn_normal_selector"
            android:onClick="onClickLevels"
            android:shadowRadius="2.0"
            android:textColor="#00e589"
            android:textSize="20sp"
            android:textStyle="bold"
            android:enabled="false"/>

        <Button
            android:id="@+id/main_hard_button"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/btn_hard_selector"
            android:onClick="onClickLevels"
            android:shadowRadius="2.0"
            android:textColor="#00e589"
            android:textSize="20sp"
            android:textStyle="bold"
            android:enabled="false"/>
    </LinearLayout>

    <com.google.android.gms.ads.AdView

        android:id="@+id/adViewLandingPage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

</RelativeLayout>

答案 5 :(得分:0)

// Replace with your xml file code

  <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

              <LinearLayout
                     android:id="@+id/container"
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"
                     android:background="@drawable/playscreenbg"
                     tools:context="com.exp.exp.ExpActivity"
                     android:orientation="vertical">

                     <LinearLayout
                         android:id="@+id/ll_utilities"
                         android:layout_width="match_parent"
                         android:layout_height="wrap_content"
                         android:orientation="horizontal"
                         android:layout_gravity="right"
                         android:gravity="right">
                             <Button
                                 android:id="@+id/btn_highscore"
                                 android:layout_width="wrap_content"
                                 android:layout_height="wrap_content"
                               android:background="@drawable/btn_highscore_selector"
                                android:onClick="onClickLevels"
                                android:enabled="false" />
            <Button
                android:id="@+id/btn_sound"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/btn_sound_off_selector"
                android:onClick="onClickLevels"
                android:enabled="false" />

        </LinearLayout>

        <ImageView
            android:id="@+id/img_Logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp"
            android:src="@drawable/logo" />

        <LinearLayout
            android:id="@+id/ll_level_buttons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <Button
                android:id="@+id/main_easy_button"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="50dp"
                android:background="@drawable/btn_easy_selector"
                android:onClick="onClickLevels"
                android:shadowRadius="3.0"
                android:textColor="#00e589"
                android:textSize="20sp"
                android:textStyle="bold"
                android:enabled="false" />

            <Button
                android:id="@+id/main_normal_button"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="50dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/btn_normal_selector"
                android:onClick="onClickLevels"
                android:shadowRadius="2.0"
                android:textColor="#00e589"
                android:textSize="20sp"
                android:textStyle="bold"
                android:enabled="false" />

            <Button
                android:id="@+id/main_hard_button"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="50dp"
                android:layout_marginTop="5dp"
                android:background="@drawable/btn_hard_selector"
                android:onClick="onClickLevels"
                android:shadowRadius="2.0"
                android:textColor="#00e589"
                android:textSize="20sp"
                android:textStyle="bold"
                android:enabled="false" />
        </LinearLayout>
    </LinearLayout>


        <com.google.android.gms.ads.AdView 
              xmlns:ads="http://schemas.android.com/apk/res-auto"
              android:id="@+id/adViewLandingPage"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              ads:adSize="BANNER"
             android:layout_alignParentBottom="true"
              />

        </RelativeLayout>
相关问题