简体   繁体   中英

ScrollView does not scroll if I add elements to it programmatically

I have a Scrollview in my xml as follows:

<LinearLayout
        android:background="@color/white"
        android:id="@+id/postsLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            android:layout_gravity="center"
            android:id="@+id/scroll_posts">
            </ScrollView>
</LinearLayout>

And I am programatically adding elements to it as follows:

ScrollView sv = root.findViewById(R.id.scroll_posts);
        LinearLayout postsList = new LinearLayout(getContext());
        postsList.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        postsList.setLayoutParams(params);

        for (int i = 1; i < 10; i++) {
            Button b = new Button(getContext());
            final float scale = getContext().getResources().getDisplayMetrics().density;
            int width = (int) (210 * scale + 0.5f);
            int height = (int) (70 * scale + 0.5f);
            float textSize = (12 * scale + 0.5f);
            b.setWidth(width);
            b.setHeight(height);
            b.setGravity(Gravity.CENTER);
            b.setTextSize(textSize);
            b.setText("Watch Ad");
            postsList.addView(b);
        }

        sv.addView(postsList);

When I run the application all the buttons show up on the screen, but i am not able to scroll through them. How can I fix this?

You need first of all put the linear layout or a linear layout inside the scrollview like so:

  <LinearLayout
    android:background="@color/white"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:layout_gravity="center"
        android:id="@+id/scroll_posts">
        <LinearLayout
            android:layout_width="match_parent"
            android:id="@+id/postsLayout"
            android:layout_height="wrap_content"
            android:orientation="vertical">


        </LinearLayout>
        </ScrollView>
       </LinearLayout>

Then add views to the linear layout as you did but not add the layout to the scroll view.

    LinearLayout postsList = (LinearLayout)root.findViewById(R.id.postsLayout);
    //postsList.setOrientation(LinearLayout.VERTICAL);
    //LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    //postsList.setLayoutParams(params);

    for (int i = 1; i < 10; i++) {
        Button b = new Button(getContext());
        final float scale = getContext().getResources().getDisplayMetrics().density;
        int width = (int) (210 * scale + 0.5f);
        int height = (int) (70 * scale + 0.5f);
        float textSize = (12 * scale + 0.5f);
        b.setWidth(width);
        b.setHeight(height);
        b.setGravity(Gravity.CENTER);
        b.setTextSize(textSize);
        b.setText("Watch Ad");
        postsList.addView(b);
    }

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