2017-07-16 65 views
1

我想在80%的屏幕和一個按鈕中分割一個GridView,並在剩下的20%中分配一個adView。這是我的XML。在Android中佈置多個元素的最佳做法是什麼?在LinearLayout中劃分佈局的問題

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    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" 
    android:orientation="vertical" 
    android:weightSum="10" 
    tools:context="com.apptree.snapper.dashboard.DashboardActivity"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weight="8" 
     android:orientation="vertical"> 
     <GridView 
      android:id="@+id/dashboard_grid_view" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="20dp" 
      android:layout_marginRight="20dp" 
      android:layout_marginBottom="20dp" 
      android:layout_marginTop="20dp"></GridView> 
    </LinearLayout>  
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weight="2" 
     android:orientation="vertical"> 
     <Button 
      android:layout_width="match_parent" 
      android:id="@+id/dashboard_takeSnapButton" 
      android:text="@string/singin_take_snap_button" 
      android:layout_height="50dp" 
      /> 
     <com.google.android.gms.ads.AdView 
      xmlns:ads="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/dashboard_adView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      ads:adSize="BANNER" 
      ads:adUnitId="ca-app-pub-1390609726414683/7854545655" 
      android:layout_alignParentStart="true" 
      android:layout_alignParentEnd="true"> 
     </com.google.android.gms.ads.AdView> 
    </LinearLayout> 
</LinearLayout> 

但它沒有給我那個結果。我錯過了什麼?

這是輸出。

enter image description here

+1

使用PercentRelativeLayout –

回答

0

試試這個XML佈局

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

    <GridView 
     android:id="@+id/dashboard_grid_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginBottom="20dp" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="20dp"/> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <Button 
     android:id="@+id/dashboard_takeSnapButton" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:text="@string/singin_take_snap_button" 
     /> 

    <com.google.android.gms.ads.AdView 
     xmlns:ads="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/dashboard_adView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     ads:adSize="BANNER" 
     ads:adUnitId="ca-app-pub-1390609726414683/7854545655"> 
    </com.google.android.gms.ads.AdView> 
</LinearLayout> 

截屏

enter image description here

+0

Thanks @Mahdi。一種佈局已從屏幕上消失。 – TechBee

+0

@TechBee好的一分鐘 –

+0

@TechBee我的帖子已更新。再檢查一遍 –

2

當你想使用Android:體重,你必須改變你的layout_height代碼如下:

android:layout_height="0dp" 
+0

感謝@codecrazer。它說可疑的大小。 – TechBee