在ScrollView中,视图未按配置与父视图匹配

50

我有一个ViewPager和带有选项卡的ActionBar。其中一个选项卡具有一个ListView,当我点击其中一个选项时,我会添加一个子Fragment,该子Fragment是该行的详细视图。

该详细视图是一个ScrollView,其中包含一个LinearLayout。 ScrollView使用match_parent/match_parent,而其中的LinearLayout使用width=match_parentheight=wrap_content。在手机大小的模拟器上,详细视图将填充所需的屏幕大小,但在平板电脑上,即使ScrollView和LinearLayout的宽度都为match_parent,详细视图也只覆盖了屏幕宽度的一部分。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootView" style="@style/RootScrollView">

    <LinearLayout android:id="@+id/scrollContentView" style="@style/ScrollViewContent">
        ...
    </LinearLayout>
</ScrollView>

这是滚动视图的样式:

<style name="RootScrollView">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
</style>

以下是内容LinearLayout的样式:

<style name="ScrollViewContent">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:paddingLeft">15dp</item>
    <item name="android:paddingRight">15dp</item>
    <item name="android:orientation">vertical</item>
    <item name="android:background">@drawable/image_legal_paper_tile</item>
</style>

内容视图具有重复的背景image_legal_paper_tile,其为:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/legal_paper_tile" 
    android:gravity="fill_horizontal"
    android:tileMode="repeat" />

因此,图像应在水平方向上拉伸并重复,从而在背景中创建一个黄色的法律纸张。

这是在手机模拟器中列表和详细视图的外观:

enter image description here

这是在真实平板电脑上查看列表和详细信息时的外观。 黄色的法律纸碎片应该填满整个屏幕宽度,但实际上没有。

enter image description here

编辑:

这是背景法律纸图像:

enter image description here

它的宽度为320像素。 手机模拟器的宽度为480像素,图像会正确地拉伸以填满屏幕的宽度。 然后在垂直方向上按预期重复。 但是,在平板电脑上,它没有完全拉伸以填满屏幕的宽度。

平板电脑上显示的宽度不是图像的原生大小,因为它根据内容更改尺寸。 当片段首次加载时,它具有一定的宽度。 然后,我填写字段并执行计算,这会在内容视图底部添加一些文本。 该文本比现有内容更宽,因此当设置文本时,片段的宽度会增加以支持更宽的内容。

总之,不,平板电脑上的宽度不是图像的原生大小。 图像正在拉伸,只是没有完全拉伸。


你是否只使用一种布局来适配所有屏幕尺寸?你的背景图片有多大?背景图片大小是否与平板上所看到的一样?如果是这样,那么可能会出现平铺重复问题(建议查看此链接:https://dev59.com/dW855IYBdhLWcg3w6Y5q)。 - abbath
平板上显示的宽度并不是图像的原始大小,因为它会根据内容的变化而改变大小。当片段首次加载时,它有一个宽度。然后我填写字段并执行计算,这会在内容视图底部添加一些文本。该文本比现有内容更宽,因此当设置文本时,片段的宽度会增加以支持更宽的内容。 - Kenny Wyland
如果您在滚动视图的子项中的线性布局中使用了布局边距,则应将其更改为填充以使其正常工作。 - Azade Rahmati
6个回答

204

在ScrollView上使用android:fillViewport="true",对于ScrollView的子元素使用android:height="wrap_content"。 如果您想要有许多不同属性的子元素,请将一个主子元素作为容器。 将其设置为wrap_content,并将其子元素设置为match_parent

例如:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:fillViewport="true">

    <LinearLayout
        android:id="@+id/dynamic_frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/dimrix_sub_child"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:visibility="gone" >
        </LinearLayout>

        <LinearLayout
             android:id="@+id/dimrix_sub_child_21"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:visibility="gone" >
        </LinearLayout>
    </LinearLayout>
</ScrollView>
在这个例子中,我可以在代码中为每个子元素设置可见性,并且它将与您所希望的父元素匹配。

2
这对我来说非常有效。你能解释一下为什么会有效吗?我很想知道。谢谢! - Shooky

19

试着在你的ScrollView中添加android:fillViewport="true"

请记住,android:layout_height=”fill_parent”的意思是“将高度设置为父元素的高度”。当使用ScrollView时,这显然不是您想要的。毕竟,如果ScrollView的内容总是与它自身一样高,那么ScrollView就变得无用了。为了解决这个问题,您需要使用ScrollView属性,称为android:fillViewport。当设置为true时,此属性导致滚动视图的子项根据需要扩展到ScrollView的高度。当子项高于ScrollView时,该属性无效。


4

我有一个类似的问题,并用以下方法解决:

    scrollView.setFillViewport(true);

0

我已经找到了一种解决方法,但我不会接受这个答案,希望有人能找到真正的答案。

在我的列表视图片段中,onListItemClick()处理程序上,我设置了详细片段的几个属性(minWidth和minHeight)。然后在详细片段的onCreateView()方法中,我将最小宽度和高度设置为该值。

详细片段现在正确地填充了空间,并仍然允许滚动等功能。


0
如果您的 parentView 是一个 ConstraintLayout,只需将 android:layout_height="0dp" 设置为 ScrollView 的 parentView,就像这样:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout>

  <SomeView/>

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp" 
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <YourItens>

        </LinearLayout>

    </ScrollView>

  </LinearLayout>

  <SomeView .../>

</androidx.constraintlayout.widget.ConstraintLayout>

0

要将ScrollView内部布局设置为match_parent,您可以在XML中使用以下代码:

android:fillViewport="true"

你的HorizontalScrollView看起来像这样

<HorizontalScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fillViewport="true"
                >

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接