【问题标题】:PercentRelativeLayout 23.2.1 inside ScrollView incorrect zero heightPercentRelativeLayout 23.2.1 里面的 ScrollView 不正确的零高度
【发布时间】:2016-03-14 09:04:46
【问题描述】:

我有一个布局,其中包含一个 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 超过屏幕高度时。

【问题讨论】:

  • 第一个问题,为什么不使用 LinearLayout 而不是 PercentRelativeLayout?您可以使用“weightSum = 1”,然后给“layout_weight”一个浮点值(例如 0.25、0.35 等)。至于这个问题,实在是太奇怪了。我注意到它发生在 KitKat 和 Lollipop 上,但不是 Marshmallow。 PercentRelativeLayout 的参考中有一些内容说“...如果您希望视图能够占用比百分比值允许的更多的空间,您可以添加 layout_width/height='wrap_content' ...”。我会深入挖掘,看看我能想出什么。
  • 我的实际视图层次结构更复杂,我不想使用嵌套权重,因为它们不利于性能,我更喜欢此屏幕的 RelativeLayout 以获得更好的定位选项,并且我还需要基于百分比的边距这将需要具有权重的 LinearLayout 中的空视图。

标签: android android-recyclerview android-percentrelativelayout


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    碰巧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%"/>
    

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

    【讨论】:

      【解决方案3】:

      虽然这不是关于如何解决 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多