如何使GridView在横向模式下运行良好

时间:2015-10-11 14:30:51

标签: android gridview

我在使GridView在横向模式下运行良好时遇到了问题。它的android:stretchMode="columnWidth"似乎在横向模式下效果不佳。

人像模式

enter image description here

正如你所看到的,我有20x20平方的网格,很适合屏幕。 (虽然右边有缺陷,但我现在可以忽略它)

<?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:background="#ffffffff"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start"
            android:id="@+id/start_button" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Random"
            android:id="@+id/random_button" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Clear"
            android:id="@+id/clear_button" />
    </LinearLayout>

    <GridView
        android:id="@+id/grid_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numColumns="20"
        android:verticalSpacing="2dp"
        android:horizontalSpacing="2dp"
        android:paddingLeft="2dp"
        android:paddingTop="2dp"
        android:paddingBottom="2dp"
        android:stretchMode="columnWidth"
        android:background="#ffc0c0c0"
        android:scrollbars="none"
        android:layout_gravity="center"
        android:gravity="center" />
</LinearLayout>

横向模式

enter image description here

然而,20x20方格网格不适合屏幕。我不希望网格视图可滚动。我想在整个屏幕上看到整个20x20平方网格。

<?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:background="#ffffffff"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start"
            android:id="@+id/start_button" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Random"
            android:id="@+id/random_button" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Clear"
            android:id="@+id/clear_button" />
    </LinearLayout>

    <GridView
        android:id="@+id/grid_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numColumns="20"
        android:verticalSpacing="2dp"
        android:horizontalSpacing="2dp"
        android:paddingLeft="2dp"
        android:paddingTop="2dp"
        android:paddingBottom="2dp"
        android:stretchMode="columnWidth"
        android:background="#ffc0c0c0"
        android:scrollbars="none"
        android:layout_gravity="center"
        android:gravity="center" />

</LinearLayout>

我可能知道我做错了什么,这使得横向模式看起来不如肖像模式好吗?

注意,为了使网格视图项看起来是正方形,我使用以下自定义类作为网格视图项。

public class SquareTextView extends TextView {

    public SquareTextView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public SquareTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public SquareTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }
}

完整的源代码可以从https://github.com/yccheok/GameOfLife

看到

1 个答案:

答案 0 :(得分:0)

我有同样的问题并通过扩展GridView(没有android:stretchMode)来解决它。在我自己的GridView中,我将以下代码放在onMeasure上:

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (getContext().getResources().getConfiguration().orientation == 1)
            super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        else {
            int myWidth = ((View)getParent().getParent()).getHeight();
            if (myWidth > 0) {
                View parent = (View)getParent();
                myWidth -= parent.getPaddingBottom() + parent.getPaddingTop();
            }
            int measure = MeasureSpec.makeMeasureSpec(myWidth, MeasureSpec.EXACTLY);
            super.onMeasure(measure, measure);
        }
    }

诀窍是获取myWidth的最顶层视图并将所有填充添加到myWidth