概念描述
一、Toolbar
作用:用来替换actionBar,使用前将系统自带的actionBar去掉
去actionBar:1.修改清单文件 android:theme="@style/Theme.AppCompat.Light.NoActionBar"
2.设置单个页面: onCreate方法中: requestWindowFeture(Window.FEAUTURE_NO_TITLE)
代替actionBar: setSupportActionBar(toolBar对象);
用法:
1添加标题setTitle();
2添加副标题setSubTitle();
3添加logo();setLogo();
4添加导航按钮setNavigationIcon(R.mipmap.xx);
5添加导航监听:toolBar:setNavigationOnClickListener(重写onClick());
二、drawerLayout:实现抽屉效果的控件
用法:
1写布局 主界面和左滑动界面嵌套(android:layout_gravity="right);
2将drawerLayout和toolbar绑定
ActionBarToggle toggle = new ActionBarToggle(上下文,drawer对象,toolbar对象,R.string.xx,R.string.xx);
toogle.syncState();同步状态
drawerLayout.addDrawerListener(toggle);抽屉添加监听
代码片段
主类
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去掉系统自带的actionBar
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
//设置自定义的actionBar
setSupportActionBar(toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.openLayout, R.string.closeLayout);
toggle.syncState();
drawer.addDrawerListener(toggle);
}
/**
*封装控件
*/
public void initView() {
toolbar = findViewById(R.id.toolbar);
drawer = findViewById(R