151228 侧滑菜单:Drawerlayout和ActionBar 一起使用的,侧滑菜单:SlidingPaneLayout,NavigationView,TabLayout


一:侧滑菜单:Drawerlayout和ActionBar 一起使用的
二:侧滑菜单:SlidingPaneLayout
三:NavigationView 需要导入v4包
四:TabLayout

TabLayout和ViewPager联动的。


侧滑菜单都是在V4包下面的rawerLayout,v4没有xml文件,所有编辑器没有办法提示出来。

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
android:layout_gravity="left|start" 注意一定要写这句话,不然会重叠在一起:

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


可以写成start,因为在阿拉伯国家是从右往左的。
但是低版本的是没有办法的。
现在的兼容性的写法是left|start


<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/drawer"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F00"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"
        />


</FrameLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:orientation="horizontal"
        android:background="#0F0"
        android:id="@+id/menu"
        >
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>






实现控制:


public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private DrawerLayout drawer;
    private LinearLayout menu;


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


        drawer = (DrawerLayout) findViewById(R.id.drawer);
        menu = (LinearLayout) findViewById(R.id.menu);
        menu.setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {
        //drawer.closeDrawer(menu);
        //drawer.closeDrawer(Gravity.START);//4.0一下是不能使用START的
        drawer.closeDrawer(GravityCompat.START);//这样就可以了
    }
}


---------------------------------------------------------------




Drawerlayout和ActionBar 一起使用的






public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private DrawerLayout drawer;
    private LinearLayout menu;
    private ActionBarDrawerToggle toggle;


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


        drawer = (DrawerLayout) findViewById(R.id.drawer);
        menu = (LinearLayout) findViewById(R.id.menu);
        menu.setOnClickListener(this);
        //显示Home
toggle = new ActionBarDrawerToggle(this, drawer, 0, 0);
        //三条横线的Menu
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toggle.syncState();
drawer.setDrawerListener(toggle);//让DrawerLayout的动作影响上面的按钮



    }




    /**
     * toggle点击后DrawerLayout 变化
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(toggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }




    @Override
    public void onClick(View v) {
        //drawer.closeDrawer(menu);
        //drawer.closeDrawer(Gravity.START);//4.0一下是不能使用START的
        drawer.closeDrawer(GravityCompat.START);//这样就可以了
    }
}




----------------------------------------------------------------------
android.support.v4.widget. SlidingPaneLayout


调换上面例子的LinearLayout 和FrameLayout


Metr design中所有的空间都是有厚度的。
在5.0以上,所有的控件都有自己的高度,z轴,在Metr design 轴越高,可以覆盖z轴低的控件。



增加一个属性:android:elevation="5dp"


<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.kodulf.cehua.Cehua2SlidingPaneLayout">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:orientation="horizontal"
        android:background="#0F0"
        android:id="@+id/menu"
        >
    </LinearLayout>


    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F00"
        android:elevation="5dp"

        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hello"
            />
    </FrameLayout>
</android.support.v4.widget.SlidingPaneLayout>




-------------------------------------------------------------------


package com.kodulf.cehua;


import android.support.v4.widget.SlidingPaneLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;


public class Cehua2SlidingPaneLayout extends AppCompatActivity implements View.OnClickListener, SlidingPaneLayout.PanelSlideListener {


    private View content;
    private SlidingPaneLayout sliding;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cehua2_sliding_pane_layout);
        sliding = (SlidingPaneLayout) findViewById(R.id.sliding);
        content = findViewById(R.id.framelayout);
        sliding.setPanelSlideListener(this);
    }




    @Override
    public void onClick(View v) {


    }




    @Override
    public void onPanelSlide(View panel, float slideOffset) {
        //slideOffset全部打开是1,完全关闭时0


        //缩放的锚点,是左侧的中心点,默认的锚点坐标是在中心点上面,那么我们改变X就行了。
        content.setPivotX(0);



        //获取屏幕宽度的一半是不可以的
//        WindowManager manager = getWindowManager();
//        Display defaultDisplay = manager.getDefaultDisplay();
//        DisplayMetrics outMetrics = new DisplayMetrics();
//        defaultDisplay.getMetrics(outMetrics);
//        int heightPixels = outMetrics.heightPixels;
//        content.setPivotY(heightPixels / 2);
        //获取控件的宽度的一半就可以了
        int height = panel.getHeight();
        content.setPivotY(height / 2);


        content.setScaleX(1- slideOffset*0.5f);

        content.setScaleY(1 - slideOffset*0.5f);


    }


    @Override
    public void onPanelOpened(View panel) {


    }


    @Override
    public void onPanelClosed(View panel) {


    }
}


---------------------------------------------


//如果要兼容2.0的手机
        ViewCompat.setPivotX(content, 0);
        ViewCompat.setPivotX(content, height / 2);
        ViewCompat.setScaleX(content, 1 - slideOffset * 0.5f);
        ViewCompat.setScaleY(content, 1 - slideOffset * 0.5f);


------------------------------------------------------------------------------
API的最低版本,google 说了存世的是10.


----------------------------------------------
package com.kodulf.cehua;


import android.support.v4.view.ViewCompat;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.BoringLayout;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;


public class Cehua2SlidingPaneLayout extends AppCompatActivity implements View.OnClickListener, SlidingPaneLayout.PanelSlideListener {


    private View content;
    private SlidingPaneLayout sliding;
    private View menu;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cehua2_sliding_pane_layout);
        sliding = (SlidingPaneLayout) findViewById(R.id.sliding);
        content = findViewById(R.id.framelayout);
        sliding.setPanelSlideListener(this);


        menu = findViewById(R.id.menu);
    }




    @Override
    public void onClick(View v) {


    }


    @Override
    public void onPanelSlide(View panel, float slideOffset) {
        //slideOffset全部打开是1,完全关闭时0


        //缩放的锚点,是左侧的中心点,默认的锚点坐标是在中心点上面,那么我们改变X就行了。
        content.setPivotX(0);


        //获取屏幕宽度的一半是不可以的
//        WindowManager manager = getWindowManager();
//        Display defaultDisplay = manager.getDefaultDisplay();
//        DisplayMetrics outMetrics = new DisplayMetrics();
//        defaultDisplay.getMetrics(outMetrics);
//        int heightPixels = outMetrics.heightPixels;
//        content.setPivotY(heightPixels / 2);
        //获取控件的宽度的一半就可以了
        int height = panel.getHeight();
        content.setPivotY(height / 2);




        content.setScaleX(1 - slideOffset * 0.5f);
        content.setScaleY(1 - slideOffset * 0.5f);


        //如果要兼容2.0的手机
//        ViewCompat.setPivotX(content, 0);
//        ViewCompat.setPivotX(content, height / 2);
//        ViewCompat.setScaleX(content, 1 - slideOffset * 0.5f);
//        ViewCompat.setScaleY(content, 1 - slideOffset * 0.5f);


 //      ViewCompat.setTranslationX(menu,-menu.getWidth()/2*(1-slideOffset));
    }


    @Override
    public void onPanelOpened(View panel) {


    }


    @Override
    public void onPanelClosed(View panel) {


    }
}






-------------------------------------------------------------------------------------


NavigationView
要导入一个包。 F4,选中dependency 选择+  选中 design()


修改Main_activity 里面的LinearLayout 为 <android.support.design.widget.NavigationView


<android.support.design.widget.NavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:id="@+id/menu"
        >
    </android.support.design.widget.NavigationView>


添加自定义属性:
 添加这行xmlns:app="http://schemas.android.com/apk/res-auto"
 
然后 Close Project,再打开。


新建一个menu 的目录
新建一个menu的文件。navigation_menu


  <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="第一项" android:id="@+id/item_1"/>
<item android:title="第二项" android:id="@+id/item_2"/>
<item android:title="第三项" android:id="@+id/item_3"/>
<item android:title="第四项" android:id="@+id/item_4"/>
</menu>




添加 app:menu="@menu/navigation" 到下面


    <android.support.design.widget.NavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:id="@+id/menu"
       
        app:menu="@menu/navigation"
        >
    </android.support.design.widget.NavigationView>


修改:MainAcitivty:
package com.kodulf.cehua;


import android.support.design.widget.NavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements View.OnClickListener, NavigationView.OnNavigationItemSelectedListener {


    private DrawerLayout drawer;
    private NavigationView menu;
    private ActionBarDrawerToggle toggle;


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


        drawer = (DrawerLayout) findViewById(R.id.drawer);
        menu = (NavigationView) findViewById(R.id.main_menu);
//        menu = (LinearLayout) findViewById(R.id.menu);
//        menu.setOnClickListener(this);
//        显示Home
        toggle = new ActionBarDrawerToggle(this, drawer, 0, 0);
        //三条横线的Menu
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toggle.syncState();
        drawer.setDrawerListener(toggle);//让DrawerLayout的动作影响上面的按钮
        menu.setNavigationItemSelectedListener(this);
    }


    /**
     * menu点击后DrawerLayout 变化
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(toggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onClick(View v) {


    }


    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        //return false;
        switch (item.getItemId()){
            case R.id.item_1:
                Toast.makeText(this,"first",Toast.LENGTH_LONG).show();
                break;
            case R.id.item_4:
                ActivityCompat.finishAffinity(this);
                break;
        }
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
//
//    @Override
//    public void onClick(View v) {
//        //drawer.closeDrawer(menu);
//        //drawer.closeDrawer(Gravity.START);//4.0一下是不能使用START的
//        drawer.closeDrawer(GravityCompat.START);//这样就可以了
//    }


}












把上面四项作为单选项:


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item android:title="第一项" android:id="@+id/item_1"/>
        <item android:title="第二项" android:id="@+id/item_2"/>
        <item android:title="第三项" android:id="@+id/item_3"/>
        <item android:title="第四项" android:id="@+id/item_4"/>
        </group>
</menu>


默认选中
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item android:title="第一项" android:id="@+id/item_1" android:checked="true"/>
        <item android:title="第二项" android:id="@+id/item_2"/>
        <item android:title="第三项" android:id="@+id/item_3"/>
        <item android:title="第四项" android:id="@+id/item_4"/>
        </group>
</menu>




添加图片
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item android:title="第一项" android:id="@+id/item_1" android:icon="@mipmap/ic_launcher" android:checked="true" />
        <item android:title="第二项" android:id="@+id/item_2" android:icon="@mipmap/ic_launcher"/>
        <item android:title="第三项" android:id="@+id/item_3" android:icon="@mipmap/ic_launcher"/>
        <item android:title="第四项" android:id="@+id/item_4" android:icon="@mipmap/ic_launcher"/>
        </group>
</menu>




添加小分组:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item android:title="第一项" android:id="@+id/item_1" android:icon="@mipmap/ic_launcher" android:checked="true" />
        <item android:title="第二项" android:id="@+id/item_2" android:icon="@mipmap/ic_launcher"/>
        <item android:title="第三项" android:id="@+id/item_3" android:icon="@mipmap/ic_launcher"/>
        <item android:title="第四项" android:id="@+id/item_4" android:icon="@mipmap/ic_launcher"/>
        </group>
    <item android:title="小分组">
        <menu>
            <item android:id="@+id/group_1" android:title="设置"></item>
            <item android:id="@+id/group_2" android:title="我的"></item>
            <item android:id="@+id/group_3" android:title="退出"></item>
        </menu>
    </item>
</menu>



将字体设置大一些:app:itemTextAppearance="?android:textAppearanceLarge"


<android.support.design.widget.NavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:id="@+id/main_menu"


        app:menu="@menu/navigation"
        app:itemTextAppearance="?android:textAppearanceLarge"
        >


---------------------------------------------------------
新建一个layout menu_header


高度设置一个定值:




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="200dp"
    android:gravity="center"
    android:background="#600F">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用户名"
        />
</LinearLayout>




然后添加app:headerLayout="@layout/menu_header"
<android.support.design.widget.NavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:id="@+id/main_menu"
        app:headerLayout="@layout/menu_header"
        app:menu="@menu/navigation"
        app:itemTextAppearance="?android:textAppearanceLarge"
        >








+++++++++++++++++++++++++++++++++++++++++++++
ViewPager


<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/drawer"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<!--FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F00"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"
        />


</FrameLayout-->


    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F00"
        android:orientation="vertical"
    >
    <android.support.v4.view.ViewPager
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pager"
        />


</LinearLayout>
    <android.support.design.widget.NavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:id="@+id/main_menu"
        app:headerLayout="@layout/menu_header"
        app:menu="@menu/navigation"
        app:itemTextAppearance="?android:textAppearanceLarge"
        >
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>


----------------------------------------------------------------
一种新的ViewPager和Fragment结合的写法:
1:Fragment
2:Adapter



---------------
Fragment:


/**
 * A simple {@link Fragment} subclass.
 */
public class BlankFragment2 extends Fragment {


    public BlankFragment2() {
        // Required empty public constructor
    }


    public static BlankFragment2 newInstance(String text){
        Bundle args = new Bundle();
        BlankFragment2 blankFragment2 = new BlankFragment2();
        args.putString("text", text);
        blankFragment2.setArguments(args);
        Log.d("151228MY", "BlankFragment postion" + text);
        return  blankFragment2;
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank_fragment2, container, false);


    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TextView text =(TextView)view.findViewById(R.id.fragment2_textView);
        String s = getArguments().getString("text");
        text.setText("HELLO"+s);
    }
}


---------------
Fragment Layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.kodulf.cehua.fragments.BlankFragment2"
    >


    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/fragment2_textView"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment"
        android:textSize="50dp"
        android:textColor="#00F"/>


</FrameLayout>
-----------------
Adapter:
package com.kodulf.cehua;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;


import com.kodulf.cehua.fragments.BlankFragment2;


import java.util.List;


/**
 * Created by Administrator on 15-12-28.
 */
public class MyAdapter extends FragmentPagerAdapter {


    private List<String> list;


    public MyAdapter(FragmentManager fm, List<String> list) {
        super(fm);
        this.list=list;
    }


    @Override
    public Fragment getItem(int position) {
        Log.d("151228MY", "postion" + position);
        return BlankFragment2.newInstance(list.get(position));
        //return null;
    }


    @Override
    public int getCount() {
//        return 0;
        return list.size();
    }


    @Override
    public CharSequence getPageTitle(int position) {
        //return super.getPageTitle(position);
        return list.get(position);
    }
}


-------------------
Main_acvitity(该代码已经实现了tab 和Viewpage的互动了)


public class MainActivity extends AppCompatActivity implements View.OnClickListener, NavigationView.OnNavigationItemSelectedListener {
//public class MainActivity extends AppCompatActivity {


    private DrawerLayout drawer;
    private NavigationView menu;
    private ActionBarDrawerToggle toggle;
    private ViewPager viewPager;
    private TabLayout tab;


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


        drawer = (DrawerLayout) findViewById(R.id.drawer);
        menu = (NavigationView) findViewById(R.id.main_menu);
//        menu = (LinearLayout) findViewById(R.id.menu);
//        menu.setOnClickListener(this);
//        显示Home
        toggle = new ActionBarDrawerToggle(this, drawer, 0, 0);
        //三条横线的Menu
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toggle.syncState();
        drawer.setDrawerListener(toggle);//让DrawerLayout的动作影响上面的按钮
        menu.setNavigationItemSelectedListener(this);
        viewPager = (ViewPager) findViewById(R.id.pager);
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < 50; i++) {
           list.add(String.valueOf(i));
        }
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager(),list));
        tab = (TabLayout)findViewById(R.id.tablayout);
        tab.setupWithViewPager(viewPager);
    }


    /**
     * menu点击后DrawerLayout 变化
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(toggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onClick(View v) {


    }


    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        //return false;
        switch (item.getItemId()){
            case R.id.item_1:
                Toast.makeText(this,"first",Toast.LENGTH_LONG).show();
                break;
            case R.id.item_4:
                ActivityCompat.finishAffinity(this);
                break;
        }
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }


//    @Override
//    public void onClick(View v) {
//        //drawer.closeDrawer(menu);
//        //drawer.closeDrawer(Gravity.START);//4.0一下是不能使用START的
//        drawer.closeDrawer(GravityCompat.START);//这样就可以了
//    }


}


-----------------
activity_main


<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/drawer"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<!--FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F00"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"
        />


</FrameLayout-->


    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F00"
        android:orientation="vertical"
    >
        <android.support.design.widget.TabLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tablayout"
            app:tabTextColor="#000"
            app:tabSelectedTextColor="#f00"
            app:tabMode="scrollable"
            app:tabIndicatorColor="#00E">


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


</LinearLayout>
    <android.support.design.widget.NavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:id="@+id/main_menu"
        app:headerLayout="@layout/menu_header"
        app:menu="@menu/navigation"
        app:itemTextAppearance="?android:textAppearanceLarge"
        >
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>






----------------------------------------------------------------
TabLayout


        <android.support.design.widget.TabLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tablayout"
            app:tabTextColor="#000"
            app:tabSelectedTextColor="#f00"
            app:tabMode="scrollable"
            app:tabIndicatorColor="#00E">


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




Main
        tab = (TabLayout)findViewById(R.id.tablayout);
        tab.setupWithViewPager(viewPager);


Adapter里面:
    @Override
    public CharSequence getPageTitle(int position) {
        //return super.getPageTitle(position);
        return list.get(position);
    }


你可能感兴趣的:(tablayout,DrawerLayout,NavigationView,千雅爸爸)