【问题标题】:View in ScrollView isn't matching parent as configuredScrollView 中的视图与配置的父级不匹配
【发布时间】:2013-03-17 20:59:17
【问题描述】:

我有一个 ViewPager 和一个带有标签的 ActionBar。其中一个选项卡有一个 ListView,当我点击该 ListView 中的一个选项时,我添加了一个子 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" />

因此,图像应该水平拉伸并重复,这会在背景中创建一个黄色的法律纸垫。

这是列表和详细视图看起来像手机模拟器的样子:

这是真实平板电脑上的列表和详细信息视图。黄色的便笺簿片段应该填满屏幕的整个宽度,但事实并非如此。

编辑:

这是法律文件的背景图片:

它的宽度为 320 像素。手机模拟器的宽度为 480 像素,并且图像可以正确拉伸以填充屏幕的宽度。然后它按预期垂直重复。但是,它不会拉伸以填充平板电脑屏幕的宽度。

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

所以,简而言之,不,平板电脑上的宽度不是图像的原始尺寸。图像正在拉伸,只是没有一直拉伸。

【问题讨论】:

  • 是否对所有布局尺寸只使用一种布局?你的背景图片有多大?背景图像大小与平板电脑上看到的一样吗?那么它可能是一个瓷砖重复问题(可能检查这个link)。
  • 平板电脑上显示的宽度不是图像的原始大小,因为它会根据内容改变大小。当片段第一次加载时,它是一个宽度。然后我填写字段并执行在内容视图底部添加一些文本的计算。该文本比现有内容更宽,因此当设置文本时,片段的宽度会增加以支持更宽的内容。
  • 如果您在滚动子项的线性布局中使用了布局边距,则应将其更改为 padding 才能正常工作。

标签: android android-fragments


【解决方案1】:

在 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】:

尝试将 android:fillViewport="true" 添加到您的 ScrollView

请记住,android:layout_height="fill_parent" 的意思是“将高度设置为父级的高度”。这显然不是您在使用 ScrollView 时想要的。毕竟,如果 ScrollView 的内容总是和它自己一样高,它就会变得毫无用处。要解决此问题,您需要使用名为 android:fillViewport 的 ScrollView 属性。当设置为 true 时,如果需要,此属性会使滚动视图的子视图扩展到 ScrollView 的高度。当child高于ScrollView时,该属性无效。

【讨论】:

    【解决方案3】:

    我遇到了类似的问题并以这种方式解决了它:

        scrollView.setFillViewport(true);
    

    【讨论】:

      【解决方案4】:

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

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

      然后细节片段正确地填充空间并且仍然允许滚动等。

      【讨论】:

        【解决方案5】:

        如果您的 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>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-25
          • 1970-01-01
          • 2020-12-18
          • 2016-04-12
          • 1970-01-01
          相关资源
          最近更新 更多