android - 查看寻呼机换行内容问题

标签 android android-viewpager android-wrap-content

我正在尝试使用此 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/

相关文章:

java - 如何通过单击按钮滑动 fragment

android - 如何在 Fresco 库中将 DraweeView 的尺寸设置为 "wrap_content"

android - 尝试在 Android 应用程序上为 layout-land 添加 XML 文件

javascript - 停止在 Url 重定向 android 上加载网页

覆盖viewpager内容的Android导航栏

Android 查看传呼机 : onCreateView getting called for both fragments

Android 将图像适配到只需要的空间

Android Horizo​​ntal RecyclerView wrap_content

java - ShortcutInfo 和 ShortcutInfoCompat 之间的区别?

java - 无法使用查询从解析数据库中检索字符串