简体   繁体   中英

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

When I try to add an imageView programmatically to the scrollView the bottom image gets cut off and I can't scroll down any further.

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

This is the java code for it:

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);

Here is an image of what is happening at the bottom of the scrollView and it no longer scrolls down.

在此处输入图片说明

I have tried to remove the fillViewport, and I have also tried to change it to add a Linear layout in the the constraint layout to see if its a problem with the constraint layout but it doesn't seem to work.

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

尝试调用scrollView.requestLayout()layout.requestLayout()加入后ImageView的。

I solved this problem by wrapping the ScrollView in a ConstraintLayout and setting the scrollView height and width to 0dp as this allows it to expand to the size of the constraintLayout and then remove this line android:fillViewport="true" and also set the second constraintLayout to match-parent for both width and height.

Incase I wasn't clear here is the updated code:

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

and the Java code:

    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);

Note this should also work with the NestedScrollView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM