Method 1: baseActivity does not have its own layout, use include to implement
1. Create a toolbar layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
2. Include write in the required interface layout
3. Create baseActivity, the general base writing method, in oncreate
setContentViewsetContentView(getLayoutId()); 添加布局
initToolBar(); 初始化toolbar,
getIntentData(); intent传递数据
initView(); 初始化控件
initData(); 初始化数据
4. Set initToolBar
mToolbar = (Toolbar) findViewById(R.id.toolbar);
if (mToolbar != null) {
//将Toolbar显示到界面
setSupportActionBar(mToolbar);
}
5. Add the control method of toolbar
5,添加toolbar的控制方法
1,toolbar是否存在
public Toolbar getToolbar() {
return (Toolbar) findViewById(R.id.toolbar);
}
2,设置标题
public void setToolBarTitle(CharSequence title) {
getToolbar().setTitle(title);
setSupportActionBar(getToolbar());
}
3,是否显示后退键
protected boolean isShowBacking(){
return true;
}
4,后退方法
showBack(){
getToolbar().setNavigationIcon(R.mipmap.icon_back);
getToolbar().setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
}
5,在onstart里面判断
onstart(){
if(null != getToolbar() && isShowBacking()){
showBack();
}
}
Method 2, baseactivity has its own layout, implemented with inflate
Under each activity, there is a LinearLayout (DecorWindow) loaded with a titleView and a ContentView. In fact, this titleView is our actionBar, so to
use ToolBar, the theme must be set to NoActionBar. We call setContentView in the activity. This method is actually to put our The layout is added to the ContentView.
initToolbar(mtoolBar);// After the root layout is initialized and the sub-layout is added to mContentView, the toolbar can be operated, otherwise there will be an error when operating the control.
When setting the title, use getSupportActionBar().setTitle();
use ToolBar, the theme must be set to NoActionBar. We call setContentView in the activity. This method is actually to put our The layout is added to the ContentView.
1. Create a toolbar layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
</FrameLayout>
2. Define variables
private Toolbar mtoolBar;
private LinearLayout mDectorView = null;//根布局
private FrameLayout mContentView = null;//activity内容布局
3. Initialize the layout, initialize the root layout of mDectorView, add the toolbar (see 4 for the initialization of the toolbar), and add mContentView
private void initDectorView() {
mDectorView = new LinearLayout(this);
mDectorView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
mDectorView.setOrientation(LinearLayout.VERTICAL);
addToolBar();
mContentView = new FrameLayout(this);
mContentView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
mDectorView.addView(mContentView);
}
4 Initialize toolbar, public property initialization
protected void addToolBar() {
View view = getLayoutInflater().inflate(R.layout.toolbar_layout, mDectorView);
mtoolBar = (Toolbar) view.findViewById(R.id.toolbar);
mtoolBar.setTitleTextColor(Color.parseColor("#ff00ff"));
}
5. Rewrite setContentView, add sub-layout into mContentView, super.setContentView (with layout)
public void setContentView(@LayoutRes int layoutResID) {
getLayoutInflater().inflate(layoutResID, mContentView);
super.setContentView(mDectorView);
}
6. After the layout is added, you can write some public classes.
getToolbar();
initToolbar(mtoolBar);
7, the structure in onCreate
if (mDectorView == null) {
initDectorView();//初始化跟布局(添加toolbar,添加mContentview给子布局留空间)
}
//如果已经创建就先把内容清空,再添加
if (mContentView != null) {
mContentView.removeAllViews();//mContentview清空里面的view
}
setContentView(getLayoutId());//把子布局添加进mContentView
setSupportActionBar(mtoolBar);//布局渲染完了之后,才能setSupportActionBar
getIntentData();//intent数据
initView();//初始化控件
initData();//初始化数据
initToolbar(mtoolBar);// After the root layout is initialized and the sub-layout is added to mContentView, the toolbar can be operated, otherwise there will be an error when operating the control.
When setting the title, use getSupportActionBar().setTitle();