2011-01-10 83 views
0

我想一个图像添加到网格视图的LinearLayout然后到线性layout.i尝试下面的代码如何添加gridview的图像中的机器人

protected LinearLayout asLayout(final String message,final String path,boolean back){ 
    LoaderImageView liv=new LoaderImageView(this,path); 
    imageView = new ImageView(mContext); 
    imageView.setLayoutParams(new GridView.LayoutParams(100,120)); 
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
    (imageView).setPadding(10, 10, 10, 10); 
    liv.setBackgroundColor(0xFF000000); 




     linearWrapper = new GridView(mContext); 

    linearWrapper.addView(asLayout(fmsg,fpath,true)); 
      linearWrapper.addView(asLayout(smsg,spath,false)); 
     linerLayout.addView(linearWrapper); 

加入在GridView但在网格视图中的图像不所以请告诉我如何将gridview图像添加到linearlayout的解决方案。

在此先感谢

问候

回答

0

您需要为网格视图的适配器。请参阅 this以了解如何添加适配器。

编辑1:示例代码:

public class TestGrid extends Activity { 
    int[] myImages; 
    @Override public void OnCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     GridView mGridView = new GridView(this); 
     /** Set up your data array with resource id's from your app. */ 
     setAdapter(new TestAdapter()); 

     setContentView(mGridView); 
    } 

    private class BenchAdapter extends BaseAdapter { 
    @Override public int getCount() { return (mContent != null) ? myImages.length : -1; } 
    @Override public Object getItem(int pos) { return pos; } 
    @Override public long getItemId(int pos) { return pos; } 
    @Override public View getView(int pos, View view, ViewGroup parent) { 
     if (myImages == null) return null; 
     GridView.LayoutParams lp = null; 
     if (getWidth() < getHeight()) lp = new GridView.LayoutParams(getWidth()/3, getHeight()/2); 
     else lp = new GridView.LayoutParams(getWidth()/2, getHeight()/3); 

     ImageView iv = new ImageView(TestGrid.this); 
     iv.setBackgroundResource(myImages[pos]); 
     iv.setLayoutParams(lp); 

     return iv; 
     } 
} 

现在这是什么会做的是建立一个GridView作为活动内容视图。适配器将填充gridview的内容。没有适配器,gridview将不知道它应该显示什么。

+0

我也有类似的疑问。为什么我需要一个适配器?适配器仍然让我困惑。 – Anirudh 2011-06-01 17:16:20

相关问题