ScrollView中的视图与配置的父级不匹配

Ken*_*and 30 android android-fragments

我有一个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>
Run Code Online (Sandbox Code Playgroud)

这是滚动视图的样式:

<style name="RootScrollView">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
</style>
Run Code Online (Sandbox Code Playgroud)

以下是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>
Run Code Online (Sandbox Code Playgroud)

内容视图有一个重复的背景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" />
Run Code Online (Sandbox Code Playgroud)

因此,图像应水平拉伸并重复,从而在背景中创建黄色合法纸垫.

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

在此输入图像描述

这是真实平板电脑上的列表和详细视图.黄色的合法垫片段应该填满整个屏幕的宽度,但事实并非如此.

在此输入图像描述

编辑:

这是背景法律文件图像:

在此输入图像描述

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

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

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

Jes*_*rix 125

在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>
Run Code Online (Sandbox Code Playgroud)

就是这个例子我可以在每个孩子的代码中设置可见性,他会根据你的意愿匹配父.

  • 谢谢 !你刚刚救了我这么多个小时! (7认同)
  • 在我的情况下,将fillViewport设置为true对我有帮助. (3认同)
  • 这对我来说非常有效。你有什么解释为什么这有效,我很想知道。谢谢! (2认同)

Pan*_*iya 8

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

请记住,android:layout_height ="fill_parent"表示"将高度设置为父级的高度."这显然不是您在使用ScrollView时所需要的.毕竟,如果ScrollView的内容总是和它本身一样高,那么它将变得毫无用处.要解决此问题,您需要使用名为android:fillViewport的ScrollView属性.设置为true时,如果需要,此属性会使滚动视图的子项扩展到ScrollView的高度.当孩子比ScrollView高时,该属性无效.


ucM*_*dia 5

我有一个类似的问题并这样解决:

    scrollView.setFillViewport(true);
Run Code Online (Sandbox Code Playgroud)