DrawerLayout与Toolbar
mainactivity布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.day09.MainActivity"
android:orientation="vertical">
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--抽屉布局-->
<LinearLayout
android:layout_gravity="left"
android:layout_width="300dp"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回抽屉"
android:id="@+id/bt"/>
<ListView
android:id="@+id/lv"
android:background="#038EFD"
android:layout_width="wrap_content"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
<!--主界面布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:background="#FF1000"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp">
<RelativeLayout
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="消息"
android:textColor="#fff"
android:textSize="20sp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/add"
android:id="@+id/iv"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
activity中的代码
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
DrawerLayout drawerLayout;
Button button;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = findViewById(R.id.drawer_layout);
button = findViewById(R.id.bt);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawerLayout.closeDrawer(Gravity.LEFT);
Toast.makeText(MainActivity.this, "返回成功!", Toast.LENGTH_SHORT).show();
}
});
initToolBar();
bindToolBar();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void initToolBar() {
toolbar = findViewById(R.id.toolbar);
//设置导航图标
toolbar.setNavigationIcon(R.mipmap.ic_launcher);
//设置toolbar的大标题
toolbar.setTitle("我是标题");
toolbar.setLogo(R.mipmap.ic_launcher_round);
//设置toolbar的小标题
toolbar.setSubtitle("我是内容");
}
//toolbar和drawerLayout绑定实现切换效果
private void bindToolBar() {
//设置开关
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
//开启同步
toggle.syncState();
//设置给drawerLayout
drawerLayout.addDrawerListener(toggle);
}
}
SlidingMenu第三方
需要导入一个slide包,如下:
SlidingMenu代码如下:
public class MainActivity extends AppCompatActivity {
SlidingMenu slidingMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
slidingMenu = new SlidingMenu(this);
slidingMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);
slidingMenu.setMode(SlidingMenu.LEFT);
View view = LayoutInflater.from(this).inflate(R.layout.layout, null);
Button button = view.findViewById(R.id.bt_close);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
slidingMenu.showContent();
}
});
slidingMenu.setMenu(view);
slidingMenu.setBehindOffset(200);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
}
}