ランダムに色付けされた四角形を含むGridViewを作成したいと思います.RetativeLayoutに配置して、グリッドの上下のボタンでグリッドの状態(つまり、色特定の四角形の)。私はこれらの色の四角形を作成し、それらをグリッドに配置する方法を考え出すのに問題があります。色付き四角形の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
そのようにアダプタを実装すると、画像ビューは背景色の上に独自の色を持つ画像であるため、背景色を持つことはできません。イメージビューには単なる色のイメージが表示されますか?これに私はいくつかのコードを書くことができる – weakwire
それは私が比較的新しいユーザーのstackoverflowだから私を許可しません:(笑 – HJM