【问题标题】:setImageResource or setImageDrawable doesn't show imagesetImageResource 或 setImageDrawable 不显示图像
【发布时间】:2014-08-14 08:03:45
【问题描述】:

我正在尝试在布局中设置图像资源/drawable。但是,当我设置图像时,输出中没有显示图像,也没有看到错误。

    View rootView = inflater.inflate(R.layout.fragment_card_layout, container, false);
    LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    cardView = layoutInflater.inflate(R.layout.recent_conversions_list, null);
    ListView listView = (ListView) rootView.findViewById(android.R.id.list);

    ...

    ImageView imageView = (ImageView) cardView.findViewById(R.id.unit_icon_ring);
    imageView.setImageResource(R.drawable.ic_angle_icon);

    ...

    return rootView;

还值得指出的是,我正在为其设置图像资源的 ImageView 不包含在片段中使用的主布局中。 R.layout.fragment_card_layout 将 R.layout.recent_conversions_list 设置为列表项。

【问题讨论】:

    标签: java android layout


    【解决方案1】:

    使用静态资源文件“R”包含对布局规范的引用,而不是对象。将 R.layout.recent_conversions_list 设置为列表视图的容器是不够的 - 它会构建它自己的副本。

    您需要创建一个 ArrayAdapater 并在那里为每个项目设置图像:

    http://geekswithblogs.net/bosuch/archive/2011/01/31/android---create-a-custom-multi-line-listview-bound-to-an.aspx

    在你的情况下,ArrayAdapter 应该有这样的一部分:

     ImageView imageView = (ImageView) convertView.findViewById(R.id.unit_icon_ring);
     imageView.setImageResource(R.drawable.ic_angle_icon);
    

    或者如果你使用 ViewHolder 模型,那么你的 ViewHolder 对象应该使用 viewHolder.setImage(R.drawable.ic_angle_icon) 类型的方法设置一些 ImageView 变量。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 2011-09-01
    相关资源
    最近更新 更多