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

标签 android android-recyclerview android-percentrelativelayout

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

在运行时,当我用数据填充 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 超过屏幕高度时。

最佳答案

不幸的是,百分比布局不适用于 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

关于android - ScrollView 内的 PercentRelativeLayout 23.2.1 不正确的零高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35983279/

相关文章:

android - PercentRelativeLayout 更高效?

android - 如何在 CollapsingToolbarLayout 中使用 PercentRelativeLayout?

android - 当 RecyclerView 位于 SwipeRefreshLayout 内时,如何使用 Behavior 滚动 RecyclerView 时显示/隐藏 FloatingActionButton?

android - 如何使用 Percent 支持库设置 ImageView 宽度并保持宽高比?

java - 为什么我恢复应用程序后 SurfaceView 会被剪切?

android - 如何将相机结果作为数据文件夹中的 uri 获取?

android - PagerAdapter 内的 RecyclerView 不更新内容

android - Recyclerview 与 GridLayout Manager 在另一个 recyclerview 中

android - 将项目转移到另一个环境后无法使用 Facebook 和 Gmail 登录

android - 以编程方式将选项卡添加到选项卡主机