水草水草 2014-11-17 08:29 采纳率: 0%
浏览 7281

ANDROID Fragment中动态创建ImageView

在一个ACTIVITY (CXmain) 中包含N个Fragment.现在在其中一个Fragment中需要动态创建imageview 但是运行时却出错.
目的是在在Fragment中创建一个可以滑动显示图片的功能 利用ScrollView来实现.下面是代码:

public class CtFragment extends Fragment  implements OnClickListener {
......................
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ...................
    layout = (LinearLayout) getActivity().findViewById(R.id.layout);

    for(int i = 0; i < 8; i++)
    {
        //  通过资源文件来获得指定一个Drawable对象
        Drawable drawable = getResources().getDrawable(R.drawable.fmj);
        ImageView imageView = new ImageView(getActivity());
        imageView.setImageDrawable(drawable);
        layout.addView(imageView);
    }

是不是getActivity() 这样用不正确......

  • 写回答

3条回答 默认 最新

  • save4me 2014-11-18 16:19
    关注

    ImageView imageView = new ImageView(getActivity());这样的用法应该可以。
    滑动显示图片参考一下 Fragment兼容手机与平板实现3D画廊和下拉列表(附源码)

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作