重写setContentView实现多个Activity部分UI布局相同

3u0h1g3r 9年前

 安卓应用中不同的activity一般都具有相同的地方,最典型的是标题栏(titlebar),我们只需在每个activity中调用setTitle就可以得到一个除了标题文字不同,其他完全相同的标题栏。

系统已经为我们引进了titlebar这样的功能,但是如果我们还需要一个类似titlebar这样容易copy外形的bottombar呢?

当然是否需要一个bottombar是个问题,我要说的其实是如果我们想让activity共享一部分UI的情况下该怎么做。

很直观的我们会写一个activity的子类,然后将公共部分的UI在这个子类activity中实现,命名为BaseActivity,最后所有要共享此部分UI的activity都继承这个BaseActivity。

思路是这样,但是究竟该如何写这个BaseActivity呢,注意上面蓝色那句话,公共部分的UI如果是通过setContentView来渲染的话那该如果处理BaseActivity子类中其独有的UI呢,合理的情况是在BaseActivity子类中调用setContentView来显示自己独有的界面,但是两次调用setContentView总有一次是会被覆盖的。

现在的情况是,我们想得到公共的UI,但没办法把公共部分和独有部分的UI分开来处理。解决问题的办法是了解activity的布局到底是如何组成的,setContentView做了些什么。

一、DecorView为整个Window界面的最顶层View。

二、DecorView只有一个子元素为LinearLayout。代表整个Window界面,包含通知栏,标题栏,内容显示栏三块区域。

三、LinearLayout里有两个FrameLayout子元素。

 (20)为标题栏显示界面。只有一个TextView显示应用的名称。也可以自定义标题栏,载入后的自定义标题栏View将加入FrameLayout中。

 (21)为内容栏显示界面。就是setContentView()方法载入的布局界面,加入其中。

所以要实现activity具有公共部分的UI,重写setContentView()方法:

import android.app.Activity;  import android.os.Bundle;  import android.view.LayoutInflater;  import android.view.View;  import android.view.ViewGroup;  import android.view.View.OnClickListener;  import android.view.ViewGroup.LayoutParams;  import android.widget.LinearLayout;  import android.widget.TextView;  public class BaseActivity extends Activity {      private TextView mTitleTx;      private View mBack;      private LinearLayout contentLayout;                                                                                                                                                                                                                                                                                    @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          initContentView();          initTitleBar();      }      public void  initTitleBar(){          mTitleTx = (TextView)findViewById(R.id.titlebar_title);            mBack = findViewById(R.id.titlebar_left);          mBack.setOnClickListener(new OnClickListener(){              @Override              public void onClick(View view){                  finish();              }          });      }                                                                                                                                                                                                                                                                                    private  void initContentView() {          ViewGroup content = (ViewGroup) findViewById(android.R.id.content);          content.removeAllViews();          contentLayout=new LinearLayout(this);          contentLayout.setOrientation(LinearLayout.VERTICAL);          content.addView(contentLayout);          LayoutInflater.from(this).inflate(R.layout.common_title_bar, contentLayout, true);      }                                                                                                                                                                                                                                                                                    @Override      public void setContentView(int layoutResID) {          //View customContentView = LayoutInflater.from(this).inflate(layoutResID,null);          /*this is the same result with           View customContentView = LayoutInflater.from(this).inflate(layoutResID,contentLayout, false);           */                                                                                                                                                                                                                                                                                           //contentLayout.addView(customContentView,LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);          LayoutInflater.from(this).inflate(layoutResID, contentLayout, true);                                                                                                                                                                                                                                                                                        }      @Override      public void setContentView(View customContentView) {          contentLayout.addView(customContentView);                                                                                                                                                                                                                                                                                        }        @Override      public void setTitle(CharSequence title) {          mTitleTx.setText(title);      }   }

(ViewGroup) findViewById(android.R.id.content)可以获得挂在一个activity内容部分LinearLayout。在这个LinearLayout中添加一个LinearLayout

contentLayout=new LinearLayout(this);
contentLayout.setOrientation(LinearLayout.VERTICAL);  content.addView(contentLayout);

作为新的内容区域。

接下来将公共部分的UI添加进新的内容区域。

LayoutInflater.from(this).inflate(R.layout.common_title_bar, contentLayout, true);

我这里是自定义了一个标题栏作为公共部分,其实如果是标题栏可以不如此麻烦直接用原生的titlebar就行了。

然后重写setContentView,将子类的内容区域从原本该直接挂在到android.R.id.content上面改为挂在到这个新的内容区域。代码如下:

@Override  public void setContentView(int layoutResID) {               LayoutInflater.from(this).inflate(layoutResID, contentLayout, true);                  }