我想调整imageview的大小,但我的代码不起作用。这是代码:
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {
R.drawable.kuhpps,
R.drawable.kuhpps,
R.drawable.kuhpps
};
错误代码的一部分:
@Override
public Object instantiateItem(ViewGroup container, int position){
Context context = MainActivity.this;
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.getLayoutParams().height = 30;
imageView.getLayoutParams().width = 30;
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
答案 0 :(得分:0)
您需要设置Layout Params
的高度和重量。您正在更新LayoutParams
getLayoutParams
实例,但不会更改视图。
这样做是为了更改ImageView
Layout size
。
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(300,300);
imageView.setLayoutParams(layoutParams);
答案 1 :(得分:0)
在viewpager的适配器中:
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = ActivityGallery.this;
ImageView imageView = new ImageView(context);
imageView.setLayoutParams(new ViewGroup.LayoutParams(mScreenWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
Drawable drawable = Drawable.createFromPath(Environment.getExternalStorageDirectory() + "/Android/data/" + getPackageName() +
"/"+....+".jpg");
if(drawable != null)
imageView.setImageDrawable(drawable);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
mScreenWidth是之前提取的当前屏幕宽度。