Activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="@+id/dros"
android:background="#ffffff"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--内容布局-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tablayout"
app:tabMode="scrollable"
></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewpage"
android:layout_above="@id/tablayout"
></android.support.v4.view.ViewPager>
</RelativeLayout>
<!--侧滑页面的占位,然后创建一个Fragment 添加上Activity(逻辑与布局操作) 后加载视图就行-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment"
android:layout_gravity="start"
android:background="#ffffff"
></FrameLayout>
</android.support.v4.widget.DrawerLayout>
MainActivity
package com.example.tablayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.example.tablayout.fragment.leftFragment;
public class MainActivity extends BaseActivity {
private ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//添加视图的方法
if(savedInstanceState==null){
getSupportFragmentManager().beginTransaction().add(R.id.fragment,new leftFragment()).commit();
}
}
@Override
protected int getLayoutResId() {
return R.layout.activity_main;
}
@Override
protected void initData() {
}
@Override
protected void initView() {
TabLayout tabLayout = findViewById(R.id.tablayout);
DrawerLayout drawerLayout = findViewById(R.id.dros);
ViewPager viewPager = findViewById(R.id.viewpage);
FrameLayout fragment = findViewById(R.id.fragment);
//侧拉的方法
//标记
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//中间人
toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open_drawer, R.string.close_drawer);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
// viewPager.setAdapter();
}
//点击图标,划出抽屉
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(toggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}