Android GridView 行高
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2375962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GridView row height
提问by kozyr
I have a GridView that displays images, which are, unfortunately, of different sizes. They are shown in between two lines of text:
我有一个 GridView 显示图像,不幸的是,这些图像的大小不同。它们显示在两行文本之间:
text1.1 text1.2
ImageView(IMAGE1) ImageView(IMAGE2)
text2.1 text2.2
text3.1
ImageView(IMAGE3)
text4.1
etc....
等等....
If IMAGE1 is the same height as IMAGE2, everything is fine, but if IMAGE1 is longerthan IMAGE2, text2.1 will run into text3.1 (padding doesn't seem to help much, as there's too much of it when images are of the same height).
如果IMAGE1是相同的高度IMAGE2,一切都很好,但如果IMAGE1是长比IMAGE2,text2.1会碰上text3.1(填充似乎并没有太大的帮助,因为有它的太多,当图像的一样的高度)。
I know there's a way to stretch the images in the ImageView so they are the same height, but is it possible to keep images as is and set the row heightsomehow?
我知道有一种方法可以拉伸 ImageView 中的图像,使它们具有相同的高度,但是是否可以保持图像原样并以某种方式设置行高?
回答by CommonsWare
You are in control over your row heights, by virtue of what you put in them. Since your cells appear to have more than one widget, they are presumably wrapped in a LinearLayout
or something. Set your LinearLayouts
to be some specific height, and the rows will all be that height.
您可以控制行高,这取决于您在其中放置的内容。由于您的单元格似乎有多个小部件,因此它们可能被包裹在一个LinearLayout
或其他东西中。将您的LinearLayouts
高度设置为某个特定的高度,行将全部为该高度。
Personally, I think you should be resizing your images if you are going to have text above and below each image on a per-cell basis.
就个人而言,如果您要在每个单元格的基础上在每个图像的上方和下方显示文本,我认为您应该调整图像大小。
回答by CloudWalker
Use drawable as background on Layout that is your grid cell and define that drawable with:
使用 drawable 作为网格单元格布局上的背景,并使用以下命令定义可绘制对象:
<size android:height="<some height>" />
For example:
例如:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff"/>
<size android:height="60dip" />
</shape>
回答by Chase
Here is an alternative solution that pre-measures items and sets the height of each cell to the max height in that row.
这是一个替代解决方案,它预先测量项目并将每个单元格的高度设置为该行中的最大高度。
GridView rows overlapping: how to make row height fit the tallest item?