45

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

该详细视图是一个 ScrollView,里面有一个 LinearLayout。ScrollView 是match_parent/match_parent,里面的 LinearLayout 是width=match_parentand height=wrap_content。在手机大小的模拟器上,详细视图会根据需要填充屏幕,但在平板电脑上,详细视图仅覆盖屏幕的部分宽度......即使 ScrollView 的宽度和 LinearLayout 的宽度是两者都匹配父级。

<?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 像素,并且图像可以正确拉伸以填充屏幕的宽度。然后它按预期垂直重复。但是,它不会拉伸以填充平板电脑上的屏幕宽度。

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

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

4

5 回答 5

186

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

在这个例子中,我可以在代码中为每个孩子设置可见性,它会按照你的意愿匹配父母。

于 2013-10-17T13:54:53.403 回答
17

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

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

于 2017-02-02T07:44:15.193 回答
2

我有一个类似的问题并以这种方式解决了它:

    scrollView.setFillViewport(true);
于 2020-12-30T09:57:15.083 回答
0

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

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

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

于 2013-03-18T05:53:06.877 回答
0

如果您的 parentViewConstraintLayout只是设置android:layout_height="0dp"为 parentView ScrollView,如下所示:

<?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>
于 2020-08-14T14:39:36.633 回答