1

我在 XML 中定义了一个 ScrollView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CustomScrollView
        android:id="@+id/custom_scroll_view"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginLeft="@dimen/edge_margin_x"
        android:layout_marginRight="@dimen/edge_margin_x"/>

</LinearLayout>

我正在以编程方式添加子视图(这是在 CustomScrollView.java 中,它扩展了 ScrollView):

    LinearLayout layout = new LinearLayout(getContext());
    List<String> elements = ...;

    for (String value : elements) {
      TextView textView = new TextView(getContext());
      textView.setText(value);
      layout.addView(textView);
   }

   addView(layout);

然后,稍后,我想为一些 TextView 添加填充:

textView.setPadding(100, 0, 100, 0);

填充已设置,但 TextView 只是在移动,因为没有刷新视图。我尝试了 invalidate()/requestLayout() 但我无法刷新所有内容。我应该在哪里以及在哪些元素上调用 invalidate()/requestLayout()?我的 XML 中有错误吗?

4

0 回答 0