Change View content in activity dynamically

安稳与你 提交于 2019-12-03 07:38:54

First change the view in your main layout into a view group (for example, a LinearLayout). Then you can add views to it. If you add a unique view, it will have exactly the effect you want to achieve.

class OneActivity extends MyActivity {
   @Override
   protected void initializeContent() {
      final ViewGroup viewGroup = (ViewGroup) findViewById(R.id.activity_content);
      viewGroup.addView(View.inflate(this, R.layout.some_view, null)); 
   }
}

In your case that should work. If your custom view group contained other views from higher up in the hierarchy, you can clean it before adding your custom view:

viewGroup.removeAllViews();

It works, I do exactly that in most of my projects.

An alternative is to look at the Fragments API, available for latest versions of the SDK.

You can't override class data members in Java, use methods instead

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!