我很难弄清楚如何以与可绘制文件夹中相同的方式使用资源文件夹中的图像。我搜索并发现我可以使用 getAssets() 函数,但我不知道如何使用它,我在 fragment 中有服装 ListView 。 我不想使用可绘制文件夹,因为我的 Assets 包含一些我需要的子文件夹。
这是我到目前为止所做的,但图像来自可绘制文件夹,我想用 Assets 中的图像替换它们:
public class AfricaFragment extends ListFragment{
private AssetManager assets;
/** An array of items to display in ArrayList */
String android_versions[] = new String[]{
"flag1",
"flag2",
"flag3",
"flag4"
};
Integer[] imgid={
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
assets = getActivity().getBaseContext().getAssets();
InputStream stream;
CustomListAdapter adapter=new CustomListAdapter(getActivity(), android_versions, imgid);
this.setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
/** Setting the multiselect choice mode for the listview */
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
非常感谢您的帮助。
最佳答案
从资源中获取图像:
try {
// Use image name with extension
InputStream ims = getActivity().getAssets().open("flag1.jpg");
Drawable d = Drawable.createFromStream(ims, null);
iv.setImageDrawable(d);
ims .close();
}
catch(IOException ex) {
return;
}
关于java - 在 ListFragment 中使用 asset 文件夹中的图像而不是可绘制文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50144928/