彩色方块的 GridView - Android

发布于 2024-11-25 21:54:44 字数 3578 浏览 8 评论 0原文

我想制作一个包含随机颜色方块的 GridView,并且想将其放入relativelayout 中,以便网格上方和下方的按钮可以改变网格的状态(即某些方块的颜色)。我无法弄清楚如何创建这些彩色方块并将它们放入网格中。

<?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:id="@+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>

我希望这些方块彼此相邻,这就是为什么我将网格中的垂直和水平间距分别减少到 0。

我应该以编程方式创建这些方块并使用某种适配器将它们添加到网格中吗?我只需要入门帮助。谢谢你!

我的适配器类代码:

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;
}

}

我的活动类中的调用:

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

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:id="@+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));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小…楫夜泊 2024-12-02 21:54:44

gridview 已经返回一个正方形网格。因此您可以通过适配器更改每个方块的颜色。

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;

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;
梦里兽 2024-12-02 21:54:44

我知道这是一篇旧文章,但也更改了weakwire的适配器getView:

    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;     
    } 

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;     
    } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文