GridView行高的设置

10
如何使行高适应GridView中最高的项?
因为默认情况下会失败。 enter image description here

更新:

这里是使用网格布局的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/gird"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fadeScrollbars="false"
        android:horizontalSpacing="5dip"
        android:numColumns="3"
        android:verticalSpacing="5dip" />

</LinearLayout>

这是网格项目的布局

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="@drawable/grid_item_background"
    android:textColor="#000000" />

这是网格项的背景可绘制对象。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient
        android:angle="270"
        android:endColor="#F2DBA7"
        android:startColor="#F2DBA7" />

    <corners android:radius="7dip" />

    <stroke android:width="3dip" android:color="#C90E0E"/>

    <padding
        android:bottom="5dip"
        android:left="5dip"
        android:right="5dip"
        android:top="5dip" />

</shape>

这是活动的源代码

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    import android.widget.GridView;

    public class GridActivity extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            GridView grid = (GridView) findViewById(R.id.gird);

            ArrayAdapter<String> answeredWords = new ArrayAdapter<String>(this,
                    R.layout.grid_item);
            grid.setAdapter(answeredWords);
            answeredWords.add("123456789101234567891012345678910");
            answeredWords.add("12345678910");
            answeredWords.add("12345678910123456789101234567891012345678910");
            answeredWords.add("12345678910");
            answeredWords.add("12345678910123456789101234567891012345678910");
            answeredWords.add("1234567891012345678910");
            answeredWords.add("1234567891012345678910");
            answeredWords.add("1234567891012345678910");
            answeredWords.add("1234567891012345678910");
            answeredWords.add("1234567891012345678910");
            answeredWords.add("123456789101234567891012345678910");
            answeredWords.add("123456789101234567891012345678910");
        }
}

更新: 我重写了GridView,它会找到元素行中的最大高度,并将该行中的所有元素设置为相同的高度。


嗨,你找到解决方案了吗?如果是的话,请发布出来。 - bGorle
1
嘿,你能上传你最新更新的代码吗?我遇到了同样的问题。 - habib ullah
1个回答

2

动态设置列高:如果您将ImageView用作GridView的单元格,请尝试设置ImageView的比例类型


2
你需要做的是将GridView和TextView的高度设置为wrap_content,这样它们就会自动调整大小,而你的TextView高度意味着你的GridView行高。 - Prachi
我这样做了,但是你可以看到如果一个元素在另一个元素上面,它们就开始重叠在一起。 - silentnuke
尝试使用填充和水平间距,并确保所有网格视图行高相同,我的意思是只需识别最高的项目,然后将其高度设置为所有文本视图。 - Prachi
1
不,谢谢。我重写了GridView,它会找到元素行的最大高度,并将该行中所有元素设置为相同的高度。结果可以在这里查看https://www.dropbox.com/s/4fbhh9mu8rbbxrr/Screenshot_2012-06-05-10-11-40.png。 - silentnuke
4
你能将你在GridView中为满足自己需求所做的更改发布一下吗?你可以将它作为对自己问题的回答发布(我会投票支持的^^)。 - elgui
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接