为什么添加 imageView 后我的 ScrollView 会中断?

Why does my ScrollView cut off after an imageView is added?

当我尝试以编程方式将 imageView 添加到 scrollView 时,底部图像被截断,我无法再向下滚动。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:id="@+id/VVV"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    >


    <android.support.constraint.ConstraintLayout
        android:id="@+id/testLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/large_text"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/testImg"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@mipmap/ic_launcher"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/text1" />


    </android.support.constraint.ConstraintLayout>
</ScrollView>

这是它的 java 代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    ConstraintSet set = new ConstraintSet();

    ImageView imageView1 = new ImageView(this);

    imageView1.setImageResource(R.mipmap.ic_launcher_round);


    TextView textView = (TextView)findViewById(R.id.text1);

    int imgId = 100+1;

    imageView1.setId(imgId);

    imageView1.setAdjustViewBounds(true);

    ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.testLayout);

    layout.addView(imageView1);

    set.clone(layout);

    set.connect(imgId, ConstraintSet.TOP, textView.getId(),ConstraintSet.BOTTOM);

    set.connect(imgId, ConstraintSet.LEFT, PARENT_ID,ConstraintSet.LEFT);

    set.connect(imgId, ConstraintSet.RIGHT, PARENT_ID,ConstraintSet.RIGHT);

    set.constrainWidth(imgId,ConstraintSet.MATCH_CONSTRAINT);

    set.constrainHeight(imgId, ConstraintSet.WRAP_CONTENT);

    set.connect(R.id.testImg, ConstraintSet.TOP, imgId , ConstraintSet.BOTTOM);

    ScrollView scrollView = (ScrollView) findViewById(R.id.VVV);

    scrollView.setFillViewport(true);



    set.applyTo(layout);

这是 scrollView 底部发生的情况的图像,它不再向下滚动。

我试过去掉fillViewport,也试过改成在constraint layout中加一个Linear layout,看看是不是constraint layout的问题,但是好像不行。

您应该尝试将嵌套滚动视图作为根布局。

尝试在添加 imageView 后调用 scrollView.requestLayout()layout.requestLayout()

我通过将 ScrollView 包装在 ConstraintLayout 并将 scrollView 高度和宽度设置为 0dp 来解决这个问题,因为这允许它扩展到 constraintLayout 的大小和然后删除此行 android:fillViewport="true" 并将第二个 constraintLayout 的宽度和高度都设置为 match-parent

如果我不清楚这里是更新的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/VVV"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >


    <ScrollView 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="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        >

        <android.support.constraint.ConstraintLayout
            android:id="@+id/testLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <TextView
                android:id="@+id/text1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque diam magna, porta vitae est ac, euismod consectetur erat. Sed non risus bibendum, scelerisque ex congue, imperdiet ipsum. Nulla convallis sapien vel semper suscipit. Quisque aliquam elit ut accumsan auctor. Pellentesque pulvinar, sapien gravida faucibus facilisis, odio tellus luctus lacus, eget porttitor orci magna vitae tellus. Morbi sed nulla placerat, laoreet leo mattis, fermentum mauris. Nulla dictum vulputate pharetra. Quisque porttitor convallis vestibulum. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec sit amet sapien dignissim, malesuada purus a, ultricies sapien.

    Nunc pharetra ante a posuere viverra. Nam feugiat ante ut tellus tincidunt bibendum. In dignissim massa eu diam aliquam tincidunt. Donec ac quam condimentum, fermentum dui feugiat, dictum nunc. Integer placerat et ex sit amet ultrices. Nunc a porta dolor. Suspendisse in risus placerat, semper sem vitae, pretium nulla. Donec fermentum diam eget lacus efficitur scelerisque. Quisque at ipsum at ante fermentum finibus.
    us. Etiam lorem augue, mattis et purus vel, accumsan aliquet mi. Integer in elementum elit. Nullam ornare ex leo, quis volutpat purus ultrices eu. Sed posuere ac tellus venenatis tincidunt. Duis egestas erat dapibus ligula finibus, a commodo enim congue. Nunc elit tellus, sagittis et arcu vel, euismod egestas est."
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                />


            <ImageView
                android:id="@+id/testImg"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@mipmap/ic_launcher"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/text1" />


        </android.support.constraint.ConstraintLayout>
    </ScrollView>

</android.support.constraint.ConstraintLayout>

和Java代码:

    ConstraintSet set = new ConstraintSet();

    ImageView imageView1 = new ImageView(this);


    imageView1.setImageResource(R.drawable.testscreen);


    TextView textView = (TextView)findViewById(R.id.text1);

    int imgId = 100+1;

    imageView1.setId(imgId);

    imageView1.setAdjustViewBounds(true);

    ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.testLayout);

    layout.addView(imageView1);

    set.clone(layout);

    set.connect(imgId, ConstraintSet.TOP, R.id.text1,ConstraintSet.BOTTOM);

    set.connect(imgId, ConstraintSet.LEFT, PARENT_ID,ConstraintSet.LEFT);

    set.connect(imgId, ConstraintSet.RIGHT, PARENT_ID,ConstraintSet.RIGHT);

    set.constrainWidth(imgId,ConstraintSet.MATCH_CONSTRAINT);

    set.constrainHeight(imgId, ConstraintSet.WRAP_CONTENT);

    set.connect(R.id.testImg, ConstraintSet.TOP, imgId , ConstraintSet.BOTTOM);

    set.applyTo(layout);

请注意,这也适用于 NestedScrollView。