简体   繁体   English

Android GridView拉伸行高度

[英]Android GridView stretch row height

I have a GridView and the items in it are not taking the full height. 我有一个GridView并且其中的项目没有占据全部高度。 It would be desired for them to stretch the same way they are doing horizontally. 希望它们以与水平操作相同的方式拉伸。 Can i achieve that? 我能做到吗?

The GridView is designed to take the remaining space in a Layout. GridView旨在占用Layout中的剩余空间。 So its height varies. 因此其高度各不相同。 But it is defined as not scrollable as there is always more than enough space. 但是它被定义为不可滚动,因为总是有足够的空间。 Is there a solution without manually calculating the height for each item? 有没有解决方案而无需手动计算每个项目的高度? Or if not can i generically calculate the desired height inside the adapter? 或者如果不能,我是否可以通用地计算适配器内部所需的高度?

The grid view is defined as follows: 网格视图的定义如下:

    <GridView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/menu_grid_view"
        android:horizontalSpacing="8dp"
        android:verticalSpacing="8dp"
        android:isScrollContainer="false"
        android:numColumns="3"
        android:scrollbars="none"
        android:stretchMode="columnWidth"/>

I also tried it with layout_height=0,layout_weight=1 on the items. 我还尝试在项目上使用layout_height=0,layout_weight=1 But it would be a nested weight and it does not work. 但这将是嵌套的权重,并且不起作用。

May be you want this to be done. 可能是您希望做到这一点。

weight will work only with linear layout. 权重仅适用于线性布局。

you can try this I hope this will help you to understand the issue why you are not getting correct layout. 您可以尝试一下,希望这可以帮助您理解为什么布局不正确的问题。

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".3"
        android:text="textview"/>

    <GridView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".7"
        android:id="@+id/menu_grid_view"
        android:horizontalSpacing="8dp"
        android:verticalSpacing="8dp"
        android:isScrollContainer="false"
        android:numColumns="3"
        android:scrollbars="none"
        android:stretchMode="columnWidth"/>

</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM