如何使用GridView和admob adView设计布局?

时间:2013-12-23 10:34:27

标签: android android-layout

我正在尝试设计一个布局,底部会有一个gridView和一个addView,但是addView显示在gridview项目上方。我希望gridview高度会自动调整屏幕

这是我的代码

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_bg_image"
android:orientation="vertical" >

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

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:fadingEdge="horizontal"
        android:horizontalSpacing="5dp"
        android:longClickable="true"
        android:numColumns="2"
        android:scrollbarAlwaysDrawVerticalTrack="true"
        android:scrollbarStyle="insideOverlay"
        android:scrollbars="horizontal"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" >
    </GridView>
</LinearLayout>

<LinearLayout
    android:id="@+id/ad_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/home_layout"
    android:layout_alignParentBottom="true"
    android:gravity="bottom"
    android:orientation="vertical" >

    <include layout="@layout/add_layout" />
</LinearLayout>

1 个答案:

答案 0 :(得分:2)

您可以对android:layout_alignParentBottom="true"使用add layout并在顶部定义此布局,然后使用GridViewadd布局下方定义android:layout_above="@id/ad_layout"。这将解决您的问题。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_bg_image"
android:orientation="vertical" >

 <LinearLayout
    android:id="@+id/ad_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="bottom"
    android:orientation="vertical" >

    <include layout="@layout/add_layout" />
 </LinearLayout>
<LinearLayout
android:id="@+id/home_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_above="@id/ad_layout" >

<GridView
    android:id="@+id/gridView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:fadingEdge="horizontal"
    android:horizontalSpacing="5dp"
    android:longClickable="true"
    android:numColumns="2"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:scrollbarStyle="insideOverlay"
    android:scrollbars="horizontal"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" >
</GridView>

 </LinearLayout>

感谢。