如何为字符串数组添加图标

时间:2016-09-29 05:04:39

标签: android string navigation

我目前正在使用一个字符串数组,我必须在导航抽屉中放置项目的图标。这是我的代码

$("body").on("DOMNodeInserted", ".ssueContentIframe", function(event) {
  $(this).css('border', '1px solid red'); //works
  console.log(event.originalEvent.originalTarget); // inserted node
});

5 个答案:

答案 0 :(得分:5)

在strings.xml中创建一个像这样的字符串数组, <integer-array name="navigation_drawables_values"> <item>@drawable/my_post_normal</item> <item>@drawable/my_engagement_icon_normal</item> <item>@drawable/my_payment_icon_normal</item> <item>@drawable/setting_icon_normal</item> <item>@drawable/about_icon_normal</item> <item>@drawable/supprot_icon_normal</item> </integer-array>

然后访问像这样的活动或碎片,

TypedArray imgs =    getResources().obtainTypedArray(R.array.navigation_drawables_values);
imgs.getResourceId(i, -1)
 mImgView1.setImageResource(imgs.getResourceId(i, -1));
imgs.recycle();

感谢@Patrick Kafka answers

答案 1 :(得分:1)

将您的图标放在mipmap文件夹中,并将其资源ID添加到string-array中,如下所示

<string-array name="icon_array">
    <item>@mipmap/ic_about_us</item>
    <item>@mipmap/ic_shopping</item>
    <item>@mipmap/icon_dining</item>
    <item>@mipmap/ic_my_city</item>
    <item>@mipmap/ic_health_fitness</item>
    <item>@mipmap/icon_education</item>
</string-array>

将它们作为<{1}}传入

Activity

TypedArray icons = context.getResources().getStringArray(R.array.discover_city_text_array); 数组传递给适配器并根据其位置设置它们

答案 2 :(得分:1)

创建一个包含drawables的整数数组,并将该数组传递给适配器。

icons

答案 3 :(得分:0)

在我的示例中,我使用Picasso将图像加载到ImageView中。

创建自己的ArrayAdapter实现,类似这样

       public class MyAdapter extends ArrayAdapter<DataModel> {

            private Context mContext;
            private Integer mResourceID;

            public MyAdapter(Context context, int resource, ) {
                super(context, resource);
                this.mContext = context;
                this.mResourceID = resource;
            }

            @Override
            public int getCount() {
                // DATA_ARRAY your data
                return DATA_ARRAY.size();
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if(v == null)
                    v = LayoutInflater.from(this.mContext).inflate(this.mResourceID,parent,false);

                DataModel model = DATA_ARRAY.get(position)

                ((TextView) v.findViewById(R.id.my_text_view)).setText(model.getStringData());
                ImageView imgIcon =  (ImageView)v.findViewById(R.id.my_image_icon);  
                // Load your icon here depending on the data in DATA_ARRAY.get(position)
                // Or you can include an int property in your DataModel which contains the icon resource
                Picasso.with(this.mContext).load(R.mipmap.ic_twitter).into(imgIcon);

                return v;
            }
        }

答案 4 :(得分:0)

ArrayList<Integer> array_image = new ArrayList<Integer>();
array_image.add(R.drawable.image1);
array_image.add(R.drawable.image2);