mipmap静态图片应用在主页

1、资源文件夹下使用

  • values下新建arrays
  
        标签绑定1
        标签盘点2
        标签登记3
        系统设置4
    

    
        @mipmap/cprk1
        @mipmap/icon_no_stock_output2
        @mipmap/icon_code_replace3
        @mipmap/xtsz4
    
  • activity代码中添加数据源
    try {
            Resources re = getResources();
            TypedArray name = re.obtainTypedArray(R.array.home_name);
            TypedArray drawable = re.obtainTypedArray(R.array.home_drawable);
            logger.e("初始化主页面个数" + name.length() + "--" + drawable.length());
            for (int i = 0; i < re.getStringArray(R.array.home_name).length; i++) {
                list.add(new HomeBean(re.getDrawable(drawable.getResourceId(i, 0)), name.getString(i)));
            }
            name.recycle();
            drawable.recycle();
        } catch (Exception e) {
            logger.e(e.getMessage());
        }

2、直接在activity中使用

    private String[] titles = new String[]
            {"功能", "系统设置"};
    private int[] drawables = new int[]{R.mipmap.logo, R.mipmap.logo};



    protected void initDatas() {
        adapter = new MainAdapter(R.layout.item_main_rcy);
        rvMian.setAdapter(adapter);
        List list = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            list.add(new MainBean(titles[i], drawables[i]));
        }
        adapter.addData(list);
    }

MainBean

public class MainBean {
    private String title;
    private Object icon;

    public MainBean() {
    }

    public MainBean(String title, Object icon) {
        this.title = title;
        this.icon = icon;
    }

    public String getTitle() {
        return title == null ? "" : title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Object getIcon() {
        return icon;
    }

    public void setIcon(Object icon) {
        this.icon = icon;
    }
}

 

你可能感兴趣的:(Android)