我已经建立了一个简单ViewPager一个与200dp的每一页上,高度为ImageView的。
I have setup a simple ViewPager that has an ImageView with a height of 200dp on each page.
下面是我的传呼机:
pager = new ViewPager(this);
pager.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
pager.setBackgroundColor(Color.WHITE);
pager.setOnPageChangeListener(listener);
layout.addView(pager);
尽管高度设置为WRAP_CONTENT,呼机总是充满即使ImageView的只是200dp屏幕。我试图替换用200在寻呼机的高度,但,让我不同结果的多种分辨率。我无法DP添加到该值。我如何200dp添加到寻呼机的布局?
Despite the height set as wrap_content, the pager always fills the screen even though the imageview is only 200dp. I tried to replace the height of the pager with "200" but that gives me different results with multiple resolutions. I am unable to add "dp" to that value. How do I add 200dp to the pager's layout?
才是硬道理onMeasure你的 ViewPager
如下将使其得到它目前拥有的最大孩子的身高
Overriding onMeasure of your ViewPager
as follows will make it get the height of the biggest child it currently has.
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = 0;
for(int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if(h > height) height = h;
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}