Android抽屉

DrawLayout和NavigationView的使用

主类代码

// An highlighted block
package com.example.lesson12_1;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        tabLayout = findViewById(R.id.main_tab);
        viewPager = findViewById(R.id.main_viewpager);


        Fragment1 fragment1 = new Fragment1();
        Fragment2 fragment2 = new Fragment2();
        Fragment3 fragment3 = new Fragment3();
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(fragment1);
        fragments.add(fragment2);
        fragments.add(fragment3);

        ArrayList<String> titles = new ArrayList<>();
        titles.add("界面一");
        titles.add("界面二");
        titles.add("界面三");

        tabLayout.setupWithViewPager(viewPager);

        MyAdapter myAdapter = new MyAdapter(getSupportFragmentManager(),fragments,titles);
        viewPager.setAdapter(myAdapter);
    }
}

布局代码

// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    tools:context=".MainActivity">

   <android.support.v4.widget.DrawerLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="vertical"
           >
           <android.support.design.widget.TabLayout
               android:id="@+id/main_tab"
               android:layout_width="match_parent"
               android:layout_height="wrap_content">

           </android.support.design.widget.TabLayout>
           <android.support.v4.view.ViewPager
               android:id="@+id/main_viewpager"
               android:layout_width="match_parent"
               android:layout_height="match_parent"></android.support.v4.view.ViewPager>

       </LinearLayout>
       <android.support.design.widget.NavigationView
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_gravity="left"
           android:background="#2a83a2"
           app:headerLayout="@layout/headview"
           >
           <LinearLayout
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:orientation="vertical"
               >
               <TextView
                   android:padding="10dp"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:text="文本框一"
                   />
               <TextView
                   android:padding="10dp"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:text="文本框二"
                   />
               <TextView
                   android:padding="10dp"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:text="文本框三"
                   />
               <TextView
                   android:padding="10dp"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:text="文本框四"
                   />
               <TextView
                   android:padding="10dp"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:text="文本框五"
                   />
           </LinearLayout>
       </android.support.design.widget.NavigationView>
   </android.support.v4.widget.DrawerLayout>
</LinearLayout>

头布局代码

// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/q4"
        />

</LinearLayout>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值