我想制作一个包含随机彩色正方形的GridView,并且我想将它放到RelativeLayout中,以便网格上下的按钮可以改变网格的状态(即颜色的某些方块)。我无法弄清楚如何创建这些彩色方块并将它们放入网格中。GridView的彩色正方形 - Android
<?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));
好极了!我现在就试过了,它非常有意义。然而,我无法看到颜色。我可能没有正确实现适配器;这是我第一次使用适配器。我现在添加我的Java代码,以便您可以看一看。 – HJM
这样你实现适配器,你不能有一个背景色,因为imageview是..以及在背景颜色的顶部有它自己的颜色的图像。图像浏览器可以显示纯色的图像吗?回答这个,所以我可以写一些代码 – weakwire
它不会让我,因为我是一个相对较新的用户stackoverflow :(大声笑 – HJM