search

Home  >  Q&A  >  body text

java - Multiple interfaces with the same layout?

Suppose I have 5 different activities, but the layout and functions at the bottom of the 5 activities are the same (other layouts are different, such as toolBar). I would like to ask how to make this same layout only written in the base class Activity Then other activities inherit this BaseActivity, thereby only writing code in one place without writing the same code in other activities.
ps: If this method cannot be achieved, is there any other method?

ringa_leeringa_lee2762 days ago1146

reply all(3)I'll reply

  • 習慣沉默

    習慣沉默2017-06-20 10:08:35

    You can read this article and see what others have done. I hope it will inspire you: http://www.jianshu.com/p/5bab...

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-06-20 10:08:35

    /q/10...
    This question of yours is similar to the one above. Please refer to it.

    reply
    0
  • 某草草

    某草草2017-06-20 10:08:35

    First we define a Boolean variable to control whether the common part is displayed, and add the method as follows:

     private boolean isShowCommonView = true;
    
      public void setShowCommonView(boolean isShow) {
            isShowCommonView = isShow;
        }

    Then we override setContentView in BaseActivity: as follows

     @Override
        public void setContentView(@LayoutRes int layoutResID) {
            super.setContentView(layoutResID);
            if (isShowBottom) {
                ViewGroup mDecorView = (ViewGroup) getWindow().getDecorView();
                //获得DecorView视图层次中的FrameLayout
                //这个地方不清楚的可以看一下Activity的视图层次图就明白了
                mContentRootView = (FrameLayout) ((ViewGroup) mDecorView.getChildAt(0)).getChildAt(1);
                mBottomView = LayoutInflater.from(getBaseContext()).inflate(R.layout.ac_main_play_view, null);
                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.gravity = Gravity.BOTTOM;
                mContentRootView.addView(mBottomView, layoutParams);
               //下面就是根据mBottomView.findViewById()来进行一些视图的初始化工作
               //如设置监听器等 
            }
        }

    Finally, in onCreate() of other subclasses that inherit BaseActivity, we must call the method setShowCommonView (Boolean) defined at the beginning before super.onCreate() to control whether the common part is displayed. As follows

    //子类的生命周期函数:onCreate(Bundle)
     @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            //显示底部视图 true为显示 false 为隐藏
            setShowBottom(true);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_mainplay);
            ButterKnife.bind(this);
            initview();
            //其他的工作。。。
        }

    reply
    0
  • Cancelreply