2014-02-10 462 views
-4

我正在開發Android應用程序,但是我遇到了GUI(GridLayout)的問題。Android網格佈局

我想知道是否可以選擇哪一列,視圖將被包含,因爲我必須填充循環中的前兩列並填充其他列。

+0

嘗試這裏http://developer.android.com/guide/topics/ui/layout/gridview.html – GhostDerfel

+0

我想用網格佈局 – user3244162

+0

也許這個例子可以幫助你:HTTP://博客.stylingandroid.com/archives/669 http://www.techotopia.com/index.php/Working_with_the_Android_GridLayout_in_XML_Layout_Resources – GhostDerfel

回答

0

上面的示例是一個gridView,其中顯示了一些圖片(mThumbIds)

通過xml創建一個gridView。

然後把它在你的代碼是這樣的:

GridView gv = (GridView) findViewById(R.id.gridView1); 

然後創建一個圖像適配器,並將其連接到您的的GridView

imageAdapter = new ImageAdapter(this); 
gv.setAdapter(imageAdapter); 

這裏代碼爲您的ImageAdapter:

對網格中的每個項目執行getView方法

public class ImageAdapter extends BaseAdapter { 
    private Context mContext; 

    public ImageAdapter(Context c) { 
     mContext = c; 
    } 

    public int getCount() { 
     return mThumbIds.length; 
    } 

    public Object getItem(int position) { 
     return null; 
    } 

    public long getItemId(int position) { 
     return 0; 
    } 

    // create a new ImageView for each item referenced by the Adapter 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView; 
     if (convertView == null) { // if it's not recycled, initialize some attributes 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(8, 8, 8, 8); 
     } else { 
      imageView = (ImageView) convertView; 
     } 

     imageView.setImageResource(mThumbIds[position]); 
     return imageView; 
    } 

    // references to our images 
    private Integer[] mThumbIds = { 
      R.drawable.sample_2, R.drawable.sample_3, 
      R.drawable.sample_4, R.drawable.sample_5, 
      R.drawable.sample_6, R.drawable.sample_7, 
      R.drawable.sample_0, R.drawable.sample_1, 
      R.drawable.sample_2, R.drawable.sample_3, 
      R.drawable.sample_4, R.drawable.sample_5, 
      R.drawable.sample_6, R.drawable.sample_7, 
      R.drawable.sample_0, R.drawable.sample_1, 
      R.drawable.sample_2, R.drawable.sample_3, 
      R.drawable.sample_4, R.drawable.sample_5, 
      R.drawable.sample_6, R.drawable.sample_7 
    }; 
}