使GridView垂直扩展

时间:2016-05-22 20:45:11

标签: android gridview

我在垂直GridView中有LinearLayout,我希望占用所有剩余的垂直空间。网格将始终包含5列和7行。网格项只是TextView。我希望网格项按比例扩展(以便每个项目是宽度的五分之一和高度的七分之一)以填充任何设备上的剩余空间。我尝试设置布局权重,但这没有做任何事情。 我怎样才能让它充满空间?

这是我的活动:

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

1 个答案:

答案 0 :(得分:0)

我并不是“按比例”理解你的意思,但是为了让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

相关问题