【问题标题】:How to load picture from @drawable to ImageView using fragments?如何使用片段将图片从 @drawable 加载到 ImageView?
【发布时间】:2017-01-02 01:43:17
【问题描述】:

如何使用片段将图片从@drawable 加载到ImageView? 在每个片段中,我都有其他图片。使用 String 数组可以使用,但使用图片则不行。它只返回 0。

XML 数组(PS。也不行)

<?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;
    }

【问题讨论】:

  • 您能否发布完整的代码和异常或错误(如果有)
  • 我没有任何错误。就是看不到图片

标签: android android-fragments android-imageview


【解决方案1】:

你想做的是:

TypedArray typedArray = res.obtainTypedArray(R.array.images);

...

image.setImageDrawable(typedArray.getDrawable(position, -1));

【讨论】:

    【解决方案2】:

    你必须做下一个:

    公共类 ShowElements 扩展片段 {

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

    public View onCreateView(LayoutInflater inflater, ViewGroup 容器, 捆绑 savedInstanceState) {

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

    public void updateArticleView(int position) { TextView autoTextView = (TextView) getActivity().findViewById( R.id.作者); autoTextView.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;
    }
    

    【讨论】:

      【解决方案3】:

      可能会在资源中定义可绘制的 id,因为数组是您的要求。但是为什么要费这么大劲才把 id 数组保存在资源中并使用 ResourcesTypedArray 并以编程方式设置它们呢?

      您可以通过编程方式定义您的可绘制 id 数组并使用它。

      public class ShowElements extends Fragment { 
           .......
           private static final int[] imageMain = new int[]{R.drawable.one,R.drawble.two};
           ......
           public View onCreateView(......) {
                   ........
           }
      }
      

      【讨论】:

        【解决方案4】:

        使用
        getResources().getDrawable(R.id.myImage);

        【讨论】:

        • 但是我想设置比一张更多的图片
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-14
        • 1970-01-01
        • 1970-01-01
        • 2021-05-16
        • 2013-09-11
        相关资源
        最近更新 更多