How to share common layout between activities without fragment

后端 未结 3 1295
挽巷
挽巷 2021-02-02 18:11

Is there any possible way to share layout(part) between activities? For example, in my app, all activities have similar layout, the top part is long operation indicator (a progr

3条回答
  •  执笔经年
    2021-02-02 18:58

    You can create an abstract 'base' activity that all your activities extend from, overriding setContentView to merge the base, and sub activity layouts.

    This way you can handle all the loading/error code in the base activity, and simply toggle between hiding and showing the views in the sub activities.

    The abstract activity:

    public abstract class BaseActivity extends Activity {
    
        protected RelativeLayout fullLayout;
        protected FrameLayout subActivityContent;
    
        @Override
        public void setContentView(int layoutResID) {
            fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_base, null);  // The base layout
            subActivityContent = (FrameLayout) fullLayout.findViewById(R.id.content_frame);            // The frame layout where the activity content is placed.
            getLayoutInflater().inflate(layoutResID, subActivityContent, true);            // Places the activity layout inside the activity content frame.
            super.setContentView(fullLayout);                                                       // Sets the content view as the merged layouts.
        }
    
    }
    

    the layout file:

    
    
    
        
        
    
        
        
    
        
    
    
    

提交回复
热议问题