如何限制一个网格布局中的ListView的高度? [英] How to limit the height of a ListView within a GridLayout?

查看:266
本文介绍了如何限制一个网格布局中的ListView的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网​​格布局包含的ListView 的TextView 。当项目添加 到的ListView 将增大,并且推的TextView 下面出来的 屏幕。我认为,通过设置 layout_gravity =补上的ListView它 不会生长在所有的,而是采取了一切可用的空间。

我如何配置的ListView 占用的可用空间,而不是 成长,推动它下面的意见出来的画面?

我要寻找一个解决方案,使用网​​格布局。我知道,在我的情况下, 的LinearLayout 将工作以及。然而,这仅仅是一个最小的测试用例 说明我的问题。

我不希望如果可能的话,设置高度以编程方式。

 < android.support.v7.widget.GridLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    应用程序:列数=1
    应用程序:useDefaultMargins =真正的>

    <的ListView
        应用程序:layout_gravity =补/>

    <的TextView
        机器人:文本=TextView的/>

< /android.support.v7.widget.GridLayout>
 

解决方案

这答案扩展我的意见的问题,并试图解释的 为什么不能使用XML通过网格布局的属性做 的。

添加的 网​​格布局 的该框架允许解决有关嵌套布局了一系列弊端,至极之一是 无法同时控制对准沿水平和垂直轴 的在文章中描述的新建布局窗口小部件:空间与网格布局通过菲利普·米尔恩

这对准控制是通过执行 layout_gravity 的参数,其<一href="http://developer.android.com/reference/android/support/v7/widget/GridLayout.LayoutParams.html#attr_android:layout_gravity"相对=nofollow> 说明 的指出参数的 指定一个组件应放在 在它的细胞群, (<强>的 不在根布局 的)。基本上,它是相同的的 的LinearLayout 的,这也是在文章中提到: 对齐/重力同样的作品就像重力的LinearLayout ...... 的正如我们知道的, LinearLayouts 的,一旦视图采取了画面的整体高度(比如 的ListView 的你的情况),它下面的意见去的屏幕如果布局的背后oriention设置为 垂直 的。

如果我们看一下 GridLayout.LayoutParams 我们将找不到任何参数这将允许内的电池组的组件粘到母体布局的特定位置,并保持在该位置独立于同一组内的其他部件的尺寸的文件;类似 layout_align [... ] 参数RelativeLayout的了。

对缺乏这种在参数我猜测的 网​​格布局 的是更好地调整控制比较嵌套布局。

我敢肯定你知道使用的方法比的 网​​格布局的 的属性,其他的解决方案(S)你的问题。我只是提出一个让答案看起来完成的:

 &LT;网格布局
    ...&GT;

    &LT; RelativeLayout的
       ...&GT;

        &LT;的TextView
            ...
            机器人:ID =@ + ID /电视
            机器人:layout_alignParentBottom =真/&GT;

        &LT;的ListView
            ...
            机器人:layout_above =@ ID /电视&GT;
         &LT; / ListView控件&GT;

    &LT; / RelativeLayout的&GT;

&LT; /网格布局&GT;
 

I have a GridLayout containing a ListView and a TextView. When items are added to the ListView it will grow in size and push the TextView below it out of the screen. I thought that by setting layout_gravity="fill" on the ListView it would not grow at all but rather take up all the available space.

How do I configure the ListView to take up the available space instead of growing and pushing views below it out of the screen?

I am looking for a solution using a GridLayout. I am aware that in my case a LinearLayout would work aswell. However this is just a minimal test case to illustrate my problem.

I do not want to set the height programmatically, if possible.

<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:columnCount="1"
    app:useDefaultMargins="true" >

    <ListView
        app:layout_gravity="fill" />

    <TextView
        android:text="TextView" />

</android.support.v7.widget.GridLayout>

解决方案

This answer extends my comments to the question and attempts to explain why this cannot be done with XML via GridLayout's attributes.

Adding GridLayout to the framework allowed solving a range of drawbacks regarding nested layouts, one of wich was "inability to control alignment along both horizontal and vertical axes simultaneously" as described in the article New Layout Widgets: Space and GridLayout by Philip Milne.

This alignment control is performed by layout_gravity parameter whose description states that the parameter "specifies how a component should be placed in its group of cells", (NOT within a root layout). Basically, it's the same as that of LinearLayout, which also mentioned in the article: "Alignment/gravity also works just like gravity in LinearLayout..." And as we know, for LinearLayouts, once a view has taken the whole height of the screen (like ListView in your case), the views below it go behind the screen if the layout oriention was set to "vertical".

If we look at the GridLayout.LayoutParams documentation we won't find any parameter which would allow a component within a cell group to stick to a certain position of the parent layout and stay at the position independent of the size of other components within the same group; similar to the layout_align[...] parameters of a RelativeLayout.

My best guess about the absence of this kind of parameters in GridLayout is better alignment control compare to nested layouts.

I'm sure you know a solution(s) for your problem using an approach other than GridLayout's attributes. I'll just suggest one to make the answer look accomplished:

<GridLayout
    ... >

    <RelativeLayout
       ... >

        <TextView
            ...
            android:id="@+id/tv"
            android:layout_alignParentBottom="true" />

        <ListView
            ...
            android:layout_above="@id/tv">
         </ListView>

    </RelativeLayout>

</GridLayout>

这篇关于如何限制一个网格布局中的ListView的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆