我希望Imageview的宽度和高度与整个屏幕相匹配。使用Layout Inflater对视图进行充气后,我在视图上设置了Layout params。但是图像的高度并未覆盖整个屏幕。这是代码:
LayoutInflater mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = mLayoutInflater.inflate(R.layout.viewpager, container, false);
itemView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
ImageView imageView = (ImageView) itemView.findViewById(R.id.viewpager_image);
/*imageView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));*/
imageView.setImageResource(mResources[position]);
container.addView(itemView);
return itemView;
这样做我的宽度与屏幕尺寸相匹配,但高度与屏幕不匹配。我正在膨胀的布局有线性布局(宽度,高度 - MATCH_PARENT)及其子图像视图(宽度,高度 - MATCH_PARENT)。我不知道我在这里做错了什么。
答案 0 :(得分:1)
DisplayMetrics displaymetrics = new DisplayMetrics();
//if in activity then use
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
//if in adapter then use
context.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
//if in fragment then use
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int height = displaymetrics.heightPixels;
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, height);
imageView.setLayoutParams(layoutParams);
答案 1 :(得分:1)
在代码中添加这些行
imageView.setScaleType(ScaleType.FIT_XY);
或
imageView.setScaleType(ScaleType.CENTER);
试试这可能有效