【问题标题】:Make a GridView expand vertically使 GridView 垂直扩展
【发布时间】:2016-05-22 20:45:11
【问题描述】:

我在垂直LinearLayout 中有一个GridView,我想占用所有剩余的垂直空间。网格将始终有 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>

【问题讨论】:

    标签: android gridview


    【解决方案1】:

    我不完全理解您所说的“按比例”是什么意思,但是要在您的示例中使 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>
    

    生成下面的布局:

    【讨论】:

    • 这是我在设计视图中看到的,但是当我在 Nexus 6P 上运行它时,我看到底部有一个很大的白色间隙。
    • 确实如此。我相信this 答案描述了您正在寻找的安排。看看吧。
    猜你喜欢
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 2020-05-27
    • 2011-05-30
    • 2019-05-05
    相关资源
    最近更新 更多