简体   繁体   English

Android中的GridView行高

[英]Gridview row height in android

I have a gridview, which gets loaded with textview and I am setting a background image for the text view. 我有一个gridview,它加载了textview,并且正在为文本视图设置背景图像。 I want to vary the textviews height depending upon some logic, but to keep same height for gridview row. 我想根据某些逻辑来改变textviews的高度,但要保持gridview行的高度不变。 I tried this code but id didn't work. 我尝试了此代码,但id无效。 tv is my textview 电视是我的textview

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

The rows overlap if I use the above above. 如果我使用以上内容,则行会重叠。 Also I tried this without setting gridview params. 我也尝试了此操作而不设置gridview参数。

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

This just creates grid view with even width and height. 这样只会创建宽度和高度均匀的网格视图。 I want constant width, but should be able to set the height according to the logic. 我想要恒定的宽度,但是应该能够根据逻辑设置高度。 Not sure where I am going wrong.I am not sure if I should go for table layout or grid layout. 不知道我要去哪里错我不确定应该去表格布局还是网格布局。 Help would be appreciated. 帮助将不胜感激。

Thanks in advance. 提前致谢。

Not sure if this will help but maybe use LinearLayout as parent for each row and textview as the child. 不知道这是否有帮助,但也许可以将LinearLayout用作每行的父级,将textview用作子级。

In grid_item.xml 在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>

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

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