Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使滚动视图填充其父视图的其余部分';还有多少空间?_Android_Scrollview_Android Constraintlayout - Fatal编程技术网

Android 如何使滚动视图填充其父视图的其余部分';还有多少空间?

Android 如何使滚动视图填充其父视图的其余部分';还有多少空间?,android,scrollview,android-constraintlayout,Android,Scrollview,Android Constraintlayout,我有两个TextViews和一个ScrollView嵌套在ConstraintLayout中。在滚动视图中有一个表格布局。ScrollView的左、右和底部约束设置为“父”。顶部约束设置为其前面的TextView。以下是XML: <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="

我有两个
TextView
s和一个
ScrollView
嵌套在
ConstraintLayout
中。在
滚动视图
中有一个
表格布局
。ScrollView的左、右和底部约束设置为“父”。顶部约束设置为其前面的TextView。以下是XML:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView6">

        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="32dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="32dp"
            android:layout_marginBottom="32dp"
            android:gravity="center"
            android:stretchColumns="0,1,2,3,4">

            <TableRow
                android:layout_width="fill_parent"
                android:layout_weight="1">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="1" />
                    ... 4 more TextViews
            </TableRow>
            ... many more TableRows
        </TableLayout>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

重叠:

当我将ScrollView高度设置为“0dp”(匹配约束)时,它会以0dp的形式呈现(ScrollView会收缩,并且不会显示任何内容)


我应该更改什么以使ScrollView扩展并填充其父容器的剩余高度?

您需要正确约束小部件,以便ConstraintLayout能够正确测量和布局视图

简言之:

  • 顶部文本视图必须是垂直链的头部(
    app:layout\u constraintVertical\u chainStyle=“packed”
    ),并且必须具有底部约束(底部到顶部…):
  • 现在它们被链接在一起,但是ScrollView需要一些操作

    只需将scrollview固定在底部,这样它就可以从某个地方拖动,并为它提供所需的0dp,这样它就可以根据约束计算其大小/位置

    <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView6">
    
    
    
    
    (同时给它一个名称/id…)

    这会导致CL正确计算空间/大小,并避免您抱怨的重叠,因为它现在对什么可以放在哪里有明确的规则


    当您忽略约束(如底部)时,特别是当有一些愚蠢的小部件(如ScrollView)时,您可能会根据内容产生不可预测的行为,这是很难预测和修复的。始终,当有疑问时,尽量给所有小部件所有的约束条件。

    我想提出几点建议(我不知道哪一点是关键的):

    1。您不应该对ConstraintLayout的子视图使用match_parent。请改用“0dp”。

    :

    注意:不能对ConstraintLayout中的任何视图使用match_parent。而是使用“匹配约束”(0dp)

    2。您不应使用
    @+id/xxx
    ,而应使用
    @id/xxx
    指定其他视图。
    前者用于创建新id

  • 将ConstraintLayout的布局高度更改为
    match\u parent

  • 将TableLayout的布局高度更改为
    wrap\u content

  • 我的结果是:

    layout.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="bottom">
    
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="16dp"
                android:text="TextView"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
            <TextView
                android:id="@+id/textView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="16dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/textView2" />
    
            <ScrollView
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/textView6">
    
                <TableLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="32dp"
                    android:layout_marginTop="32dp"
                    android:layout_marginEnd="32dp"
                    android:layout_marginBottom="32dp"
                    android:gravity="center"
                    android:stretchColumns="0,1,2,3,4">
    
                    <TableRow
                        android:layout_width="fill_parent"
                        android:layout_weight="1">
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:text="1" />
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:text="2" />
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:text="3" />
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:text="4" />
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:text="5" />
                    </TableRow>
    
                   <!-- plenty of TableRows... -->
    
                </TableLayout>
            </ScrollView>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </FrameLayout>
    
    
    
    IDE(Android Studio)本身使用
    @+id
    创建新id和引用现有id,但实际上在引用视图时是否包含加号并不重要。至于高度,将其(和宽度)设置为0dp并不能解决问题-结果如问题中所述。@WaisKamal。。。在我的版面编辑器中,它看起来不错(没有重叠)。你的意思是在你附加的照片中没有重叠吗?是否可以尝试添加更多行,使表格高于可用高度?这就是重叠发生的地方。我不确定这是否重要,但ConstraintLayout嵌套在FrameLayout中。@WaisKamal我更新了我的答案。你能显示你的代码吗?您可能遗漏了一些内容,因为我得到了完全不同的结果(使用0dp时高度为0)。此外,ConstraintLayout必须根据其内容展开(以编程方式设置的某个高度),因此将其高度设置为与父项匹配在这里并不方便。即使我这样设置,也无法正确渲染。谢谢,但问题仍然存在。0dp按字面意思呈现“0dp”(零高度)。但是0dp width渲染正确(展开以填充父级)。如果不查看全图,很难看到问题所在。0dp并不意味着零高度,它是CL使用的一个特殊值,用于知道某些小部件的大小将由其约束决定(当然,也由布局中的其他视图决定)。如果滚动视图以“0高度”渲染,则表示缺少约束,或者CL没有足够的信息来计算它;这可能是由于多种原因造成的,甚至时间也可能是其中之一;根据您何时“填充”滚动视图,也会影响此操作。非常感谢,是的,约束有问题。
    <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView6">
    
    
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="bottom">
    
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="16dp"
                android:text="TextView"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
            <TextView
                android:id="@+id/textView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="16dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/textView2" />
    
            <ScrollView
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/textView6">
    
                <TableLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="32dp"
                    android:layout_marginTop="32dp"
                    android:layout_marginEnd="32dp"
                    android:layout_marginBottom="32dp"
                    android:gravity="center"
                    android:stretchColumns="0,1,2,3,4">
    
                    <TableRow
                        android:layout_width="fill_parent"
                        android:layout_weight="1">
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:text="1" />
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:text="2" />
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:text="3" />
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:text="4" />
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:text="5" />
                    </TableRow>
    
                   <!-- plenty of TableRows... -->
    
                </TableLayout>
            </ScrollView>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </FrameLayout>