使用字符串

时间:2016-11-11 11:19:31

标签: android android-drawable android-resources

到目前为止,我使用此代码根据String值添加自定义ImageView:

RelativeLayout rl_dashboard = (RelativeLayout) mView.findViewById(R.id.rl_dashboard);
            for (int i = 0; i < imageResIDs.size(); i++) {
                int drawableResourceId = mActivity.getResources().getIdentifier(imageResIDs.get(i), "drawable", mActivity.getPackageName());
                ImageView imageView = new ImageView(mActivity);
                imageView.setImageDrawable(ContextCompat.getDrawable(mActivity, drawableResourceId));
                rl_dashboard.addView(imageView);
            }

但是,正如我想的那样,这给了我最高的res drawable,即xxxdpi atm。有没有办法拉出特定密度的可绘制的?

我正在使用以下方法检查密度:

DisplayMetrics metrics = new DisplayMetrics();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        switch(metrics.densityDpi){
            case DisplayMetrics.DENSITY_LOW:
                break;
            case DisplayMetrics.DENSITY_MEDIUM:
                break;
            case DisplayMetrics.DENSITY_HIGH:
                break;
            case DisplayMetrics.DENSITY_XHIGH:
                break;
            case DisplayMetrics.DENSITY_XXHIGH:
                break;
            case DisplayMetrics.DENSITY_XXXHIGH:
                break;
        }

我会在提取资源之前访问我的静态值。

谢谢!

1 个答案:

答案 0 :(得分:0)

ImageViews大小错误的原因是缺少LayoutParams我添加的内容:

imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));