水草水草 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画廊和下拉列表(附源码)

    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容