【发布时间】:2016-03-14 09:04:46
【问题描述】:
我有一个布局,其中包含一个 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 超过屏幕高度时。
【问题讨论】:
-
第一个问题,为什么不使用 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