如果我以编程方式向其添加元素,ScrollView 不会滚动

ScrollView does not scroll if I add elements to it programmatically

我的 xml 中有一个滚动视图,如下所示:

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

我正在以编程方式向其中添加元素,如下所示:

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

当我 运行 应用程序时,所有按钮都显示在屏幕上,但我无法滚动浏览它们。我该如何解决这个问题?

您首先需要将线性布局或线性布局放入滚动视图中,如下所示:

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

然后像您一样将视图添加到线性布局,但不将布局添加到滚动视图。

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