彩色方块的 GridView - Android
我想制作一个包含随机颜色方块的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
gridview 已经返回一个正方形网格。因此您可以通过适配器更改每个方块的颜色。
gridview already returns a grid of squares. So you can change the color of each square from the adapter.
我知道这是一篇旧文章,但也更改了weakwire的适配器getView:
I know this is an old post, but change weakwire's adapter getView too: