2016-05-22 37 views
0

我有一个GridView垂直LinearLayout,我想要占用所有剩余的垂直空间。网格将始终有5列和7行。网格物品只是TextView。我希望网格项目按比例扩展(以便每个项目是宽度的1/5和高度的七分之一)以填充任何设备上的剩余空间。我尝试设置布局权重,但那没有做任何事情。 我该如何填补空间?使GridView垂直展开

这里是我的活动:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    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" 
    android:fitsSystemWindows="true" 
    android:orientation="vertical"> 

    <TextView 
    android:text="@string/zero" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/tv0" 
    android:gravity="end" 
    android:textAppearance="@style/TextAppearance.AppCompat.Large" 
    android:paddingEnd="8dp" 
    android:paddingStart="8dp" 
    android:textColor="#e4e4e4" 
    android:background="@color/colorPrimaryDark"/> 

    <!-- A few more TextViews --> 

    <GridView 
    android:id="@+id/grid_view" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:horizontalSpacing="0dp" 
    android:verticalSpacing="0dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 
    android:numColumns="5"/> 
</LinearLayout> 

而这里的网格项目的布局:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
    android:id="@+id/text" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:background="@color/colorPrimary" 
    android:paddingBottom="15dp" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:paddingTop="15dp" 
    android:textAppearance="@style/TextAppearance.AppCompat.Medium"/> 
</FrameLayout> 

回答

0

我没exacly明白你所说的“按比例”的意思,反而使GridView “填充空间”在你的例子中你不需要设置布局权重:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/text" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/text" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/text" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/text" /> 

    <GridView 
     android:id="@+id/grid_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:horizontalSpacing="0dp" 
     android:numColumns="5" 
     android:stretchMode="columnWidth" 
     android:verticalSpacing="0dp" /> 

</LinearLayout> 

下面生成布局:

enter image description here

+1

这就是我在设计视图中看到的,但是当我在我的Nexus 6P运行它,我看到在底部一个白色的大的差距。 – kwiqsilver

+0

的确如此。我相信[this](http://stackoverflow.com/a/11049803/2776632)答案描述你正在寻找的安排。看一看。 –