2016-05-22 118 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)答案描述你正在尋找的安排。看一看。 –