开发者社区> 问答> 正文

GridView可以操作但不能显示图片

public class MainActivity extends Activity {
int [] imageIds = new int[]
{
R.drawable.one,R.drawable.two,R.drawable.three,
R.drawable.four,R.drawable.five,R.drawable.six

};
ImageSwitcher switcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();
   for (int i = 0;i<imageIds.length;i++)
   {
    Map<String,Object> items = new HashMap<String,Object>();
    items.put("iamge", imageIds[i]);
    listItems.add(items);

   }    
   switcher = (ImageSwitcher)findViewById(R.id.switcher);
   switcher.setFactory(new ViewFactory()
   {

        @Override
        public View makeView() {
            // TODO Auto-generated method stub
            ImageView imageview = new ImageView(MainActivity.this);
            imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageview.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            return imageview;
        }

   });
   SimpleAdapter simpleadapter = new SimpleAdapter(this, listItems, R.layout.cell, new String[]{"image"},new int[]{R.id.image1} );
   GridView grid01 = (GridView)findViewById(R.id.grid01);
   grid01.setAdapter(simpleadapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

展开
收起
蛮大人123 2016-06-16 17:24:02 1996 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
     public View makeView() {
            // TODO Auto-generated method stub
            ImageView imageview = new ImageView(MainActivity.this);
            imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageview.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            return imageview;
        }

    这个只是new了一个Imageview,但是没有为该ImageView设置显示内容。

    2019-07-17 19:41:30
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载