2013-05-02 79 views
0

我的ImageView正在顯示我正在製作的小遊戲的國際象棋風格棋盤圖像。它由GridView覆蓋,顯示我的作品的圖像。我有它顯示在下面,但它沒有正確定位。我希望它完全匹配GridView的邊緣,但不知道如何。有什麼方法可以使GridView成爲ImageView的「父」,那麼我可以在XML中說「match_parent」,因爲它的高度和寬度等等?簡單的方法使GridView下的ImageView與GridView的大小完全匹配?

下面是我的應用程序現在的樣子。我想要我製作的醜陋彩色網格與GridView圖像對齊,以便圍繞我的作品邊緣的黑色方塊以圖板的正方形爲中心。

enter image description here

這裏是我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/textFieldFU" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/gridview" 
    android:layout_alignTop="@+id/gridview" 
    android:layout_centerHorizontal="true" /> 

<GridView 
    android:id="@+id/gridview" 
    android:layout_width="fill_parent" 
    android:layout_height="600dp" 
    android:gravity="center" 
    android:horizontalSpacing="0dp" 
    android:numColumns="8" 
    android:stretchMode="columnWidth" 
    android:verticalSpacing="10dp" /> 

這裏是我的MainActivity的相關部分:

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    final ImageAdapter iA = new ImageAdapter(this); 

    final ImageView board; 
    board = (ImageView) findViewById(R.id.imageView1); 
    board.setImageResource(R.drawable.board1); 


    GridView gridview = (GridView) findViewById(R.id.gridview); 
    gridview.setAdapter(iA); 
... 

這裏是我的ImageAdapter相關部分我有哪些設置GridView

public class ImageAdapter extends BaseAdapter { 

public static int[] images = { R.drawable.rock2, R.drawable.paper2, 
     R.drawable.scissors2, R.drawable.rock2, R.drawable.paper2, 
...//This part goes on for a while... 
...//On to the relevant part... 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ImageView iv; 
    if (convertView != null) { 
     iv = (ImageView) convertView; 
    } else { 
     iv = new ImageView(context); 
     iv.setLayoutParams(new GridView.LayoutParams(60, 60)); 
     iv.setScaleType(ScaleType.CENTER); 
     iv.setPadding(0, 0, 0, 0); 
    } 
    iv.setImageResource(images[position]); 
    return iv; 
} 

public static int[] getImagesArray() { 
    return images; 
} 

public void setImagesArray(int[] newImages) { 
    images = newImages; 
} 

---------------- EDIT,添加從回答的替代解決方案如下.--------------

我擺脫了ImageViewImageAdapter類完全,只是添加到了我的getView的:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ImageView iv; 
    if (convertView != null) { 
     iv = (ImageView) convertView; 
    } else { 
     iv = new ImageView(context); 
     iv.setLayoutParams(new GridView.LayoutParams(60, 60)); 
     iv.setScaleType(ScaleType.CENTER); 
     iv.setPadding(0, 0, 0, 0); 
     if(position < 8 && position % 2 ==0){ 
      iv.setBackgroundColor(Color.DKGRAY); 
     } 
     else if(position > 7 && position < 16 && position % 2 ==1){ 
      iv.setBackgroundColor(Color.DKGRAY); 
     } 
     else if(position > 15 && position < 24 && position % 2 ==0){ 
      iv.setBackgroundColor(Color.DKGRAY); 
     } 
     else if(position > 23 && position < 32 && position % 2 ==1){ 
      iv.setBackgroundColor(Color.DKGRAY); 
     } 
     else if(position > 31 && position < 40 && position % 2 ==0){ 
      iv.setBackgroundColor(Color.DKGRAY); 
     } 
     else if(position > 39 && position < 48 && position % 2 ==1){ 
      iv.setBackgroundColor(Color.DKGRAY); 
     } 
     else if(position > 47 && position < 56 && position % 2 ==0){ 
      iv.setBackgroundColor(Color.DKGRAY); 
     } 
     else if(position > 55 && position < 64 && position % 2 ==1){ 
      iv.setBackgroundColor(Color.DKGRAY); 
     } 
     else 
      iv.setBackgroundColor(Color.GRAY); 
    } 
    iv.setImageResource(images[position]); 
    return iv; 
} 

這是現在的樣子:

enter image description here

+0

此答案也幫助了我,http://stackoverflow.com/questions/9062512/how-set-alternate-cell-color-in-a-grid – ChrisWilson4 2013-05-02 15:20:07

回答

1

您可以配置gridview的基本元素的佈局。 帶有自定義背景的imageView似乎是你需要的。 您必須在getview中檢查您所在的行,以瞭解要使用的背景顏色。

+0

這聽起來很有希望,我現在就檢查它。 – ChrisWilson4 2013-05-02 13:38:37

+0

你說得對,這是一個比我所做的更好的主意。我會把解決方案放在最上面。 – ChrisWilson4 2013-05-02 15:03:48

+0

很高興聽到它。它提供了擺脫圖層的額外好處。儘可能避免堆積圖層是一個好主意,因爲它會花費大量資源(系統必須繪製所有這些連續的圖層,即使它們不可見。 – Teovald 2013-05-02 15:25:07