我正在尝试使用此 Android: I am unable to have ViewPager WRAP_CONTENT 将 View 寻呼机高度设置为wrap_content
但它在底部留下了额外的空白
如何删除这个空格?
这是我的 xml 代码:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
card_view:cardCornerRadius="@dimen/card_corner_radius">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.test.android.custom_views.WrapContentHeightViewPager
android:id="@+id/similarRecipesPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:visibility="visible" />
</LinearLayout>
</android.support.v7.widget.CardView>
最佳答案
尝试一下,按如下方式覆盖 ViewPager
的 onMeasure 将使其获得当前最大子项的高度。
@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);
}
关于android - 查看寻呼机换行内容问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41629096/