如何使用片段将图片从@drawable加载到ImageView? [英] How to load picture from @drawable to ImageView using fragments?

查看:161
本文介绍了如何使用片段将图片从@drawable加载到ImageView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用片段将图片从@drawable加载到ImageView? 在每个片段中,我都有其他图片.使用String数组有效,但使用图片无效.它只返回0.

How to load picture from @drawable to ImageView using fragments? In every single fragment I have other picture. With array of String that's works, but with pictures doesn't. It's only returns 0.

XML数组(PS.也无法使用)

XML Array (PS. also doesn't work)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="images">
        <item>@drawable/one</item>
        <item>@drawable/two</item>
    </array>
</resources >

XML视图:

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="15" />

JAVA:

public class ShowElements extends Fragment {

    final static String ARG_POSITION = "position";
    int mCurrentPosition = -1;
    String[] authorsNicks;
    int[] imageMain;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
                ...
        authorsNicks = res.getStringArray(R.array.authors_nicks);
        imageMain = res.getIntArray(R.array.images);
        return...
   }

public void updateArticleView(int position) {
        TextView autorTextView = (TextView) getActivity().findViewById(
                R.id.author);
            autorTextView.setText(authorsNicks[position]);

         //image.setImageResource(R.drawable.one); It load picture
        image.setImageResource(imageMain[position]); //No picture in view
        Log.i("qwerty",String.valueOf(imageMain[position])); // 0 in LogCat

        mCurrentPosition = position;
    }

推荐答案

您必须执行下一个操作:

You have to do the next:

公共类ShowElements扩展了片段{

public class ShowElements extends Fragment {

final static String ARG_POSITION = "position";
int mCurrentPosition = -1;
String[] authorsNicks;
TypeArray imageMain;

public View onCreateView(LayoutInflater充气机,ViewGroup容器, 捆绑saveInstanceState){

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            ...

    authorsNicks = res.getStringArray(R.array.authors_nicks);
    imageMain = res.obtainTypedArray(R.array.images);
    return...    }

public void updateArticleView(int position){ TextView autorTextView =(TextView)getActivity().findViewById( R.id.author); autorTextView.setText(authorsNicks [position]);

public void updateArticleView(int position) { TextView autorTextView = (TextView) getActivity().findViewById( R.id.author); autorTextView.setText(authorsNicks[position]);

     //image.setImageResource(R.drawable.one); It load picture
    image.setImageResource(imageMain.getResourceId(position, -1)); //It should load your picture
    Log.i("qwerty",String.valueOf(imageMain[position])); // 0 in LogCat

    mCurrentPosition = position;
}

这篇关于如何使用片段将图片从@drawable加载到ImageView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆