ScrollView 内的 PercentRelativeLayout 23.2.1 不正确的零高度

PercentRelativeLayout 23.2.1 inside ScrollView incorrect zero height

我有一个布局,其中包含一个 ScrollView 和一个 LinearLayout 及其子项:一个 PercentRelativeLayout 23.2.1,另一个 LinearLayout 在它下面,另一个 PercentRelativeLayout 之后。在中间 LinearLayout 我有一个 RecyclerViewLinearLayoutManager,垂直)。您可能会在下面看到一个示例。

在运行时,当我用数据填充 RecyclerView 时,它的大小会增加,因为我从一个空数据开始。一旦它的高度改变到超过屏幕尺寸,上面的汽车(id = car)图像,其高度为百分比值,就会完全从屏幕上消失。当我上下滚动此屏幕时,我到达了顶部和底部边缘并且看不到图像。

我在这个布局中还有一个地方有百分比值,它们定义了 marginTop。这些边距也消失了。宽度相关的百分比值工作正常。 java 代码不包含图像的可见性更改,也不调整页边距。

我目前不确定这个问题是否与支持库的特定版本有关,我在发布该库的同时构建了这个屏幕。另外,我在 Marshmallow 上看不到这个问题,只在 Lollipop 及以下版本上看到。对于如何解决此问题的任何帮助,我将不胜感激。

<!-- a container for all views that should scroll -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!-- the first block of views - they need percents -->
    <android.support.percent.PercentRelativeLayout
        android:id="@+id/carHolder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/car"
            android:layout_width="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/car"
            app:layout_heightPercenet="25%"/>

        <ImageView
            android:id="@+id/sliderBg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/car"
            android:layout_centerHorizontal="true"
            android:src="@drawable/slider_bg"/>

    </android.support.percent.PercentRelativeLayout>

    <!-- a middle block of views, they don't need percents -->
     <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

         <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

    </LinearLayout>

    <!-- another block of views, they need percents -->
    <android.support.percent.PercentRelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <!-- some other views --> 

    </android.support.percent.PercentRelativeLayout>

</LinearLayout>

更新

我在支持库 23.3.0 中也看到了这个问题,不仅是当我填充 RecyclerView 时,而且每次 ScrollView 超过屏幕高度时。

虽然这不是关于如何解决 PercentRelativeLayout 问题的答案,但这是一个如何在布局中使用百分比的示例,如果您正在寻找的就是这些。

<!-- a container for all views that should scroll -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!-- the first block of views - they need percents -->
    <LinearLayout
        android:id="@+id/carHolder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:weightSum="1">

        <ImageView
            android:id="@+id/car"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="0.25"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/car" />

        <ImageView
            android:id="@+id/sliderBg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/slider_bg"/>

    </LinearLayout>

    <!-- a middle block of views, they don't need percents -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>

    <!-- another block of views, they need percents -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:weightSum="1">

        <!-- some other views -->

    </LinearLayout>

</LinearLayout>

这也是一个很好的 explanation

LinearLayout weightSum Reference

碰巧 layout_aspectRatio 参数在这里工作正常,因为我也知道视图的宽度,所以我可以通过使用此参数和基于百分比的宽度来获得首选高度。使用此解决方案,高度将取决于屏幕的宽度,这可能是不同尺寸的问题。

<ImageView
    android:id="@+id/car"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/car"
    app:layout_aspectRatio="175%"
    app:layout_widthPercent="70%"/>

我会将此答案标记为临时解决方案,并接受有助于以正确方式解决问题的任何其他答案。

不幸的是,百分比布局不适用于 M 之前的 ScrollView。原因是它们取决于测量步骤中提供的尺寸提示。在 M 之前,大多数布局在发送未指定的度量规范时会提供大小提示 0。

您可以尝试通过创建自己的 ScrollView 子类并覆盖 measureChild 和 measureChildWithMargins(幸运的是两者都受到保护)来提供大小提示来解决这个问题。 来源- plus.google.com

您可以使用此 CustomScrollView 使 percentRelativeLayout 起作用

public class CustomScrollView extends ScrollView {


public CustomScrollView(Context context) {
    super(context);
}

public CustomScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int size = 0;
    int width = getMeasuredWidth();
    int height = getMeasuredHeight();

    if (width > height) {
        size = height;
    } else {
        size = width;
    }
    setMeasuredDimension(size, size);
}
}

来源是PercentRelativeLayout inside ScrollView