无法刷新布局中的自定义视图

时间:2014-01-28 08:34:30

标签: android layout view android-linearlayout

在我的onCreateView我有以下代码将自定义视图填充到我的LinearLayout StageViewer扩展View

layout = (LinearLayout)rootView.findViewById(R.id.lo1);     

for (int i = 0 ; i < 6 ; i ++)
{

  StageViewer viewer = new StageViewer(i,callingActivity,this, 0, Global.STAGE_VIEW);
  layout.addView(viewer);
  stageList.add(viewer);        
}

在我的应用程序中,用户可以选择将所有视图设置为默认模式

public void resetToDefault()
{

  for (int i = 0 ; i < 6 ; i ++)
  {

    StageViewer viewer = new StageViewer(i,callingActivity,this, 0, Global.STAGE_VIEW);
    layout.addView(viewer);
    stageList.add(viewer);      

  }

}

但重置时,新视图不可见(显示空布局)。

我试过

viewer.invalidate();
layout.refreshDrawableState();

我错过了什么吗?

2 个答案:

答案 0 :(得分:0)

public void resetToDefault()
{
  layout.removeAllViews();
   stageList.clear();
  for (int i = 0 ; i < 6 ; i ++)
  {

    StageViewer viewer = new StageViewer(i,callingActivity,this, 0, Global.STAGE_VIEW);
    layout.addView(viewer);
    stageList.add(viewer);      
  }

}

答案 1 :(得分:0)

也许缺少布局参数?

LinearLayout.LayoutParams layoutParams =
    new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT);
viewer.setLayoutParams(layoutParams);
layout.addView(viewer);