GridView制作方板

时间:2014-12-15 12:35:19

标签: android gridview layout

我正在尝试使用gridview制作一个5 x 5网格的方板。这是我的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <GridView
        android:id="@+id/squareBoard"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:numColumns="5"
    />
</RelativeLayout>

这是我使用的适配器:

private class TextViewAdapter extends BaseAdapter {
    private Context mContext;
    private int mWidth, mHeight;

    public TextViewAdapter(Context c, int width, int height) {
        mContext = c;
        mWidth = width;
        mHeight = height;
    }

    public int getCount() {
        return mWidth * mHeight;
    }

    public TextView getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView tv = null;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            tv = new TextView(mContext);
            tv.setText("" + position);
            tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
            tv.setLayoutParams(new GridView.LayoutParams(100, 100));
            tv.setWidth(100);
            tv.setBackground(getResources().getDrawable(R.drawable.letter_box));
        } else {
            tv = (TextView) convertView;
        }

        return tv;
    }
}

这是textview background

的letter_box.xml
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"   >

    <solid
        android:color="@android:color/white" >
    </solid>

    <stroke
        android:width="1dp"
        android:color="@android:color/black" >
    </stroke>
</shape>

到目前为止,我设法显示了框,但列之间始终存在水平空间。我希望他们像棋盘一样。

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

创建自己的类MyGridView,扩展GridView

覆盖其onMeasure(...)方法并设置height == width,如下所示:

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

然后,创建自己的GridViewItem扩展View子类,例如TextViewImageView等。以同样的方式覆盖其onMeasure(...)方法。

MyGridView及其项目都显示为方形。

答案 1 :(得分:-1)

在GridView上,您可以设置两个项目之间的垂直间距和水平间距。尝试将它们设置为0dp。

<GridView
    android:id="@+id/squareBoard"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:verticalSpacing="0dp"
    android:horizontalSpacing="0dp"
    android:numColumns="5"
/>