【问题标题】:View Pager wrap content issue查看寻呼机包装内容问题
【发布时间】:2017-01-13 07:05:01
【问题描述】:

我正在尝试使用 Android: I am unable to have ViewPager WRAP_CONTENT 将视图寻呼机高度设置为 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>

【问题讨论】:

标签: android android-viewpager android-wrap-content


【解决方案1】:

试试这个,如下覆盖你的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);
}

【讨论】:

    【解决方案2】:

    如果您使用 WRAP_CONTENT 定义您的高度,并且内容不够大,您将有额外的空间,因为这是预期的行为。

    如果我的理解正确,您希望您的 ViewPager 占据您的活动/片段的所有可能空间,其他视图也在其中。

    如果您想要一个占用所有可用空间的 ViewPager,您应该使用 LinearLayoutweight 属性:

    <LinearLayout android:orientation="vertical"
        android:layout_height="match_parent"
        android:layout_width="match_parent">
    
        <ViewPager android:id="@+id/my_view_pager"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="1"/>
    
        <View
            android:id="@+id/other_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    

    这样,无论你的 viewPager 的内容是什么,它都会填满所有额外的空白。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-23
      • 2013-10-03
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多