Toolbar和DrawerLayout以及slidingMenu

效果图

在这里插入图片描述
先写布局
注意要使用drawer的话要把主布局写到drawerLayout中
必须先写主布局

<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=".MainActivity"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_alignParentLeft="true"
                android:src="@mipmap/ic_launcher"
                android:id="@+id/head1"
                android:layout_width="50dp"
                android:layout_height="50dp" />
            <ImageView
                android:layout_alignParentRight="true"
                android:src="@mipmap/ic_launcher"
                android:id="@+id/head2"
                android:layout_width="50dp"
                android:layout_height="50dp" />
        </RelativeLayout>

    </android.support.v7.widget.Toolbar>
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--主布局-->
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <FrameLayout
                android:layout_weight="1"
                android:id="@+id/frameLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </FrameLayout>
            <com.flyco.tablayout.CommonTabLayout
                android:id="@+id/tab"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </com.flyco.tablayout.CommonTabLayout>
        </LinearLayout>

        <!--侧滑布局-->
        <LinearLayout
            android:layout_gravity="left"
            android:layout_width="200dp"
            android:layout_height="match_parent">
            <RelativeLayout
                android:background="#DD1A1A"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <Button
                    android:text="按钮"
                    android:id="@+id/bt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </RelativeLayout>


        </LinearLayout>


    </android.support.v4.widget.DrawerLayout>



</LinearLayout>
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private String[] arr = {"android.permission.READ_CONTACTS",
            "android.permission.CALL_PHONE",
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE",
            "android.permission.INTERNET"};
    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private SlidingMenu slidingMenu;
    private CommonTabLayout commonTabLayout;
    private ImageView head1,head2;
    private FrameLayout frameLayout;
    private ArrayList<CustomTabEntity> list = new ArrayList<>();
    private Fragment1 fragment1 = new Fragment1();
    private Fragment2 fragment2 = new Fragment2();
    private Fragment3 fragment3 = new Fragment3();
    private Fragment4 fragment4 = new Fragment4();
    private Fragment5 fragment5 = new Fragment5();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        right();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void right() {
        if(Build.VERSION.SDK_INT >= 23){
            for(int i=0;i<arr.length;i++){
                if(ActivityCompat.checkSelfPermission(this,arr[i])!= PackageManager.PERMISSION_GRANTED){
                    ActivityCompat.requestPermissions(this,arr,1);
                }
            }

        }
    }



    private void initView() {
        toolbar = findViewById(R.id.toolbar);
        drawerLayout = findViewById(R.id.drawer);
        commonTabLayout = findViewById(R.id.tab);
        head1 = findViewById(R.id.head1);
        head2 = findViewById(R.id.head2);

        head1.setOnClickListener(this);
        head2.setOnClickListener(this);

        slide();

        commonTab();

    }

    private void commonTab() {
        list.add(new MyTab("消息",R.drawable.frown,R.drawable.frown2));
        list.add(new MyTab("好友",R.drawable.frown,R.drawable.frown2));
        list.add(new MyTab("我的",R.drawable.frown,R.drawable.frown2));
        list.add(new MyTab("首页",R.drawable.frown,R.drawable.frown2));
        list.add(new MyTab("内容",R.drawable.frown,R.drawable.frown2));
        commonTabLayout.setTabData(list);
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                commonTabLayout.hideMsg(position);
                if(position == 0){
                    getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout,fragment1).commit();
                }else if(position == 1){
                    getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout,fragment2).commit();
                }else if(position == 2){
                    getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout,fragment3).commit();
                }else if(position == 3){
                    getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout,fragment4).commit();
                }else if(position == 4){
                    getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout,fragment5).commit();
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    private void slide() {
        slidingMenu = new SlidingMenu(this);
        //滑出后屏幕的剩余大小
        slidingMenu.setBehindOffset(300);
        //从哪边滑出
        slidingMenu.setMode(SlidingMenu.RIGHT);
        //侧滑页面拉动效果面积 TOUCHMODE_FULLSCREEN全部
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
        slidingMenu.attachToActivity(this,SlidingMenu.SLIDING_CONTENT);
        slidingMenu.showContent();
        View view = LayoutInflater.from(this).inflate(R.layout.slidingmenu_layout, null);
        slidingMenu.setMenu(view);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.head1:
                head1();
                break;
            case R.id.head2:
                head2();
                break;
        }
    }

    /**
     * slidemenu侧滑
     */
    private void head2() {


        slidingMenu.showMenu();
    }

    /**
     * drawerlayout侧滑
     */
    private void head1() {
       //左侧滑出
        drawerLayout.openDrawer(Gravity.LEFT);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值