在android中的Gridview行高

时间:2014-07-11 19:24:48

标签: android gridview

我有一个gridview,它加载了textview,我正在为文本视图设置背景图像。我想根据一些逻辑改变textviews高度,但是为gridview行保持相同的高度。我试过这段代码但是id没有用。电视是我的textview

    int Height = 150 + Integer.parseInt(PostCount);
    if(Height >= MaxHeight)
    {
     Height = MaxHeight;
    }
   tv.setLayoutParams(new GridView.LayoutParams(200, Height));

如果我使用上面的内容,行会重叠。我也尝试了这个,没有设置gridview params。

    int Height = 150 + Integer.parseInt(PostCount);
    if(Height >= MaxHeight)
    {
     Height = MaxHeight;
    }
    tv.setHeight(Height);
    tv.setWidth(200);

这只会创建宽度和高度均匀的网格视图。我想要恒定宽度,但应该能够根据逻辑设置高度。不知道我哪里出错。我不确定是否应该去桌面布局或网格布局。帮助将不胜感激。

提前致谢。

1 个答案:

答案 0 :(得分:0)

不确定这是否有帮助,但可能使用LinearLayout作为每行的父级和textview作为子级。

在grid_item.xml中

<LinearLayout
     android:layout_width="xdp"    <!-- where x is the max width for each row -->
     android:layout_height="ydp"   <!-- where y is the max height for each row -->

     <TextView
     // rest of the definition for Textview

     </TextView>
</LinearLayout>
相关问题