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>