开发者

GridView of Colored Squares -- Android

开发者 https://www.devze.com 2023-03-22 06:51 出处:网络
I want to make a GridView that contains randomly colored squares, and I want to put that into a RelativeLayout so that buttons above and below the grid can alter the state of the grid (i.e the colors

I want to make a GridView that contains randomly colored squares, and I want to put that into a RelativeLayout so that buttons above and below the grid can alter the state of the grid (i.e the colors of certain squares). I'm having trouble figuring out how to create these colored squares and put them into the grid.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gameLayout"
android:orientation="vertical" 
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
>

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:i开发者_运维知识库d="@+id/colorGrid"
    android:layout_width="200dip" 
    android:layout_height="200dip"
    android:columnWidth="90dip"
    android:numColumns="auto_fit"
    android:verticalSpacing="0.0dip"
    android:horizontalSpacing="0.0dip"
    android:layout_centerHorizontal="true" />

<Button 
    android:id="@+id/redButton"
    android:layout_width="75dip"
    android:layout_height="75dip"
    android:text="RED"
    android:layout_centerHorizontal="true"
    android:layout_weight="1.0"
    android:layout_below="@id/colorGrid" />

<Button
    android:id="@+id/yellowButton"
    android:layout_width="75dip"
    android:layout_height="75dip"
    android:text="YELLOW"
    android:layout_weight="1.0"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/redButton" />

<Button
    android:id="@+id/greenButton"
    android:layout_width="75dip"
    android:layout_height="75dip"
    android:text="GREEN"
    android:layout_weight="1.0"
    android:layout_toLeftOf="@id/redButton" 
    android:layout_below="@id/colorGrid" />

<Button
    android:id="@+id/lightBlueButton"
    android:layout_width="75dip"
    android:layout_height="75dip"
    android:text="LIGHT BLUE"
    android:layout_weight="1.0"
    android:layout_toRightOf="@id/redButton" 
    android:layout_below="@id/colorGrid" />

<Button
    android:id="@+id/darkBlueButton"
    android:layout_width="75dip"
    android:layout_height="75dip"
    android:text="DARK BLUE"
    android:layout_weight="1.0"
    android:layout_toLeftOf="@id/redButton" 
    android:layout_below="@id/redButton" />

<Button
    android:id="@+id/purpleButton"
    android:layout_width="75dip"
    android:layout_height="75dip"
    android:text="PURPLE"
    android:layout_weight="1.0"
    android:layout_toRightOf="@id/redButton" 
    android:layout_below="@id/redButton" />



</RelativeLayout>

I want the squares to be right next to each other that's why I reduced the vertical and horizontal spacing in the grid to 0 each.

Should I create these squares programmatically and use an Adapter of some sort to add them to the grid? I just need help getting started. Thank you!

My adapter class code:

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class GameAdapter extends BaseAdapter{
private Context mContext;

public GameAdapter(Context c){
    mContext = c;
}

public int getCount() {

    return 10;
}

@Override
public Object getItem(int position) {

    return null;
}

@Override
public long getItemId(int position) {

    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view;
    if(convertView==null){
        view=new View(mContext);
    }else{
        view = (View) convertView;
    }
    view.setBackgroundColor(Color.rgb((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)));
    return view;
}

}

The call in my activity class:

setContentView(R.layout.gamelayout);
GridView gridView = (GridView)findViewById(R.id.colorGrid);
gridView.setAdapter(new GameAdapter(TheActivity.this));


gridview already returns a grid of squares. So you can change the color of each square from the adapter.

public int getCount() {

    return 10;
}

private int r,g,b;
r=Rand with bounds [0,255]
g=...
b=....

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


   View view;
    view=new ImageView(mContext);
    view.setBackgroundColor(Color.rgb(r, g, b));

return view;


I know this is an old post, but change weakwire's adapter getView too:

    public View getView(int position, View convertView, ViewGroup parent) {     
        if (convertView == null) {   
            convertView = new ImageView(context);
            convertView.setMinimumHeight(32);
            convertView.setMinimumWidth(32);

        }
        ((ImageView)convertView).setImageDrawable(new ColorDrawable((int) getItem(position)));
        return convertView;     
    } 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号