Change View content in activity dynamically

后端 未结 2 1116
一向
一向 2021-02-09 17:44

My abstract class extended from Activity class consists of three Views as described in the following XML snippet:



        
相关标签:
2条回答
  • 2021-02-09 18:25

    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.

    0 讨论(0)
  • 2021-02-09 18:37

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

    0 讨论(0)
提交回复
热议问题