我有一个GridLayout
包含ListView
和TextView
。将项目添加到时,ListView
它的大小会增加,并将其TextView
下方的内容推出屏幕。我认为通过layout_gravity="fill"
在ListView上进行设置,它根本不会增长,而是会占用所有可用空间。
如何配置ListView
占用可用空间,而不是将其下方的视图扩大并推出屏幕?
我正在寻找使用的解决方案GridLayout
。我知道在我的情况下aLinearLayout
也可以。但是,这只是说明我的问题的一个最小测试用例。
如果可能的话,我不想以编程方式设置高度。
<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>
该答案将我的评论扩展到该问题,并尝试进行解释why this cannot be done with XML via GridLayout's attributes
。
添加GridLayout
到框架中可以解决嵌套布局方面的一系列弊端,其中之一就是Philip Milne"inability to control alignment along both horizontal and vertical axes simultaneously"
在文章New Layout Widgets:Space and GridLayout中描述的。
这种对准控制通过执行layout_gravity
参数,其description
状态是,参数()。基本上,它与文章中也提到的相同:并且,我们知道,对于,一旦视图占据了整个屏幕的高度(如您的情况),则如果下面的视图位于屏幕后面,布局方向设置为。"specifies how a component should be placed
in its group of cells",
NOT within a root layout
LinearLayout
"Alignment/gravity also works just like gravity in LinearLayout..."
LinearLayouts
ListView
"vertical"
如果我们查看GridLayout.LayoutParams文档,我们将找不到任何参数,该参数将允许单元格组中的组件粘贴到父布局的某个位置,并停留在与该组件中其他组件的大小无关的位置团体; 类似于layout_align[...]
RelativeLayout的参数。
我最好的猜测GridLayout
是,与嵌套布局相比,对齐控制更好。
我敢肯定,您可以使用GridLayout's
属性以外的方法来解决问题。我只是建议一个使答案看起来很完整的人:
<GridLayout
... >
<RelativeLayout
... >
<TextView
...
android:id="@+id/tv"
android:layout_alignParentBottom="true" />
<ListView
...
android:layout_above="@id/tv">
</ListView>
</RelativeLayout>
</GridLayout>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句