我試圖調整viewpager到頁面的其餘部分。我有這個globallayout聽衆:Viewpager填充頁面的其餘部分
ViewTreeObserver viewTreeObserver = pager.getViewTreeObserver();
viewTreeObserver
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int viewPagerWidth = pager.getWidth();
float viewPagerHeight = Constants.screenHeight - findViewById(R.id.view_above_pager).getBottom();
Log.i("","viewpager height:" + viewPagerHeight);
layoutParams.width = viewPagerWidth;
layoutParams.height = (int) viewPagerHeight;
pager.setLayoutParams(layoutParams);
pager.setMinimumHeight((int)viewPagerHeight);
pager.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
Log.i("","viewpager height2:" + pager.getHeight());
}
});
顯然,第一個日誌,在viewPagerHeight顯示:viewpager高度:530.0 但第二個呼機上顯示爲0。爲什麼會出現這種情況?它不應該刷新高度嗎?
nope :)))在預繪製時不起作用,仍然是0 –