【问题标题】:Android: ViewGroup Overlapping When Adding Children within ActivityAndroid:在 Activity 中添加子项时 ViewGroup 重叠
【发布时间】:2013-02-14 17:14:11
【问题描述】:

我的情况如下: 在我的布局 xml 中,我有一个通用的 LinearLayout 作为主容器,我想基于一个分页系统填充内容,所有内容都包含在单个活动中。

我的Java代码如下:

private void goToPage(int pageNum){
    LinearLayout layout = (LinearLayout)this.findViewById(R.id.layoutRadioGroups);

    //Clear Views
    layout.removeAllViews();

    //Figure out array positions based on page number
    if (pageNum < this.PAGE_NUM)
        this.LAST_QUESTION_START -= this.NUM_PER_PAGE[pageNum];
    else{
        if (pageNum > 0)
            this.LAST_QUESTION_START += this.NUM_PER_PAGE[pageNum - 1];
        else
            this.LAST_QUESTION_START = 0;
    }
    this.PAGE_NUM = pageNum;

    //Grab my array values, create a TextView, and add it to the layout
    for (int i=this.LAST_QUESTION_START; i < this.LAST_QUESTION_START + this.NUM_PER_PAGE[pageNum]; i++){
        TextView tv = new TextView(layout.getContext());
        tv.setText(this.QUESTIONS[i]);
        tv.setId(i);
        layout.addView(tv);
    }
}

因此,它会清除所有视图(仅是 TextView 的),找出正确的数组值来制定 TextView 对象,然后将它们添加到我的布局中。

现在第 0 页显示正确,当我单击下一步按钮时,第 1 页也正确显示。但是当我转到第2页时,第1页的内容还在,第2页的内容覆盖在上面。

我是 Android 开发的新手,所以我不确定我没有刷新某些内容或使某些内容无效,但我很困惑。大家有什么想法吗?

谢谢!

【问题讨论】:

    标签: android android-activity overlap viewgroup


    【解决方案1】:

    找出问题所在。

    仅当您将应用程序的 android:windowBackground 设置为 @null 时才会发生此重叠问题。

    如果您只是将 textview 的文本设置为数组值,甚至会发生这种重叠问题。您不必实际将视图添加到布局或任何东西。

    故事的寓意,一开始就正确设置。

    话虽如此,有人知道为什么会发生这种情况吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      相关资源
      最近更新 更多