设置固定GridView行高

时间:2013-05-29 16:19:31

标签: android android-gridview

我在GridView中遇到行高的各种问题。

我想用XML设置项目的大小(高度/宽度),然后让GridView自动调整尽可能多,不需要拉伸。如果它不适合下一个元素,它应该只在它能够适应的当前元素数量周围添加填充。

目前我得到2列(对我来说几乎看起来固定大小)并且行被拉伸。有人可以帮助解释发生了什么以及如何实现我的目标吗?

GridView的:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/main_grid"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:paddingRight="20dp"
    android:paddingLeft="20dp"
    android:clipToPadding="true"
    android:fitsSystemWindows="true"
    android:stretchMode="none"
    android:background="@drawable/main_grid_background">

</GridView>

GridItem(我想要它320x320,因为我后来在其中插入一个背景图像,如果它不是一个完美的正方形,看起来很奇怪。)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="320dp"
    android:layout_height="320dp"
    android:padding="10dp" >

    <TextView
        android:id="@+id/grid_item_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text="@+id/label"
        android:layout_marginTop="5dp"
        android:textColor="@color/black"
        android:textSize="15sp"
        android:visibility="invisible"
        android:layout_centerInParent="true" >
    </TextView>

</RelativeLayout>

爪哇:

public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(context);

            // get layout from mobile.xml
            gridView = inflater.inflate(R.layout.main_grid_item, null);

        } else {
            gridView = (View) convertView;
        }
        TextView textView = (TextView) gridView
                .findViewById(R.id.grid_item_label);

            //SET TEXT AND BACKGROUND IMAGE HERE
            //gridView.setBackgroundResource(R.drawable.main_grid_item_import);



        return gridView;
    }

3 个答案:

答案 0 :(得分:49)

在CustomAdapter

v.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, rowHigh));

rowHigh是您必须更改的维度

答案 1 :(得分:4)

public class MyGridView extends GridView {
    public MyGridView(Context context) {
        super(context);
    }

    public MyGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightSpec;

        if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) {
            // The great Android "hackatlon", the love, the magic.
            // The two leftmost bits in the height measure spec have
            // a special meaning, hence we can't use them to describe height.
            heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);
        } else {
            heightSpec = heightMeasureSpec;
        }

        super.onMeasure(widthMeasureSpec, heightSpec);
    }


}

在XML中:

<MyGridView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/main_grid"
    android:gravity="center"/>

答案 2 :(得分:3)

所以,这不是一个完美的答案,但这就是我开始工作的方式。借鉴this主题的想法。

假设我知道我的图像尺寸为320x320,所有这些都有效。另外,在GridView中我必须设置android:columnWidth="320dp"或者它不起作用。如果有人有更好的想法,请发布它...现在我继续前进。

网格项XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp" >
    <ImageView
    android:id="@+id/grid_item_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"/>
    <TextView
        android:id="@+id/grid_item_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text="@+id/label"
        android:textColor="@color/black"
        android:layout_alignLeft="@+id/grid_item_image"
        android:layout_alignTop="@+id/grid_item_image"
        android:layout_alignRight="@+id/grid_item_image"
        android:layout_alignBottom="@+id/grid_item_image"
        android:gravity="center"
        android:textSize="15sp"
        android:visibility="invisible">
    </TextView>

</RelativeLayout>

GridView XML:

<GridView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/main_grid"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:columnWidth="320dp"
    android:paddingRight="20dp"
    android:paddingLeft="20dp"
    android:clipToPadding="true"
    android:fitsSystemWindows="true"
    android:stretchMode="columnWidth"
    android:background="@drawable/main_grid_background">
</GridView>