2011-07-23 23 views
2

我想製作一個包含隨機彩色正方形的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)); 

回答

1

的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; 
+0

好極了!我現在就試過了,它非常有意義。然而,我無法看到顏色。我可能沒有正確實現適配器;這是我第一次使用適配器。我現在添加我的Java代碼,以便您可以看一看。 – HJM

+0

這樣你實現適配器,你不能有一個背景色,因爲imageview是..以及在背景顏色的頂部有它自己的顏色的圖像。圖像瀏覽器可以顯示純色的圖像嗎?回答這個,所以我可以寫一些代碼 – weakwire

+0

它不會讓我,因爲我是一個相對較新的用戶stackoverflow :(大聲笑 – HJM

0

我知道這是舊的文章,但改變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;  
    }