Android 线性布局中的分割布局问题

Android 线性布局中的分割布局问题,android,android-layout,Android,Android Layout,我试图在80%的屏幕上划分一个GridView,在剩下的20%中划分一个按钮和一个adView。这是我的XML。在Android中布局多个元素的最佳实践是什么 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk

我试图在80%的屏幕上划分一个GridView,在剩下的20%中划分一个按钮和一个adView。这是我的XML。在Android中布局多个元素的最佳实践是什么

<?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>

但这并没有给我这个结果。我错过了什么

这是输出


当您想使用android:weight时,必须将布局高度代码更改为:

android:layout_height="0dp"
试试这个xml布局


截屏


我建议您使用PercentRelativeLayout以获得更好的性能


使用PercentRelativeLayoutThank@Mahdi。屏幕上出现了一个布局。@TechBee ok,请稍候
<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>