CoordinatorActivity.java:
package com.iwanghang.designdemo; import android.support.design.widget.Snackbar; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class CoordinatorActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_coordinator); findViewById(R.id.coordinator_btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Snackbar.make(v,"Snackbar in CoordinatorLayout 向右滑动消失",Snackbar.LENGTH_LONG).show(); Snackbar.make(v,"Snackbar in CoordinatorLayout \n向右滑动消失",Snackbar.LENGTH_LONG). setAction("exit", new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(CoordinatorActivity.this, "ActivityCompat.finishAffinity(CoordinatorActivity.this);", Toast.LENGTH_SHORT).show(); ActivityCompat.finishAffinity(CoordinatorActivity.this); // 结束 App //finish(); // 结束 CoordinatorActivity } }).show(); } }); } }activity_coordinator.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_coordinator" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.iwanghang.designdemo.CoordinatorActivity"> <Button android:id="@+id/coordinator_btn" android:text="coordinator button" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </android.support.design.widget.CoordinatorLayout>MainActivity.java:
package com.iwanghang.designdemo; import android.content.Intent; import android.support.design.widget.NavigationView; import android.support.design.widget.Snackbar; import android.support.design.widget.TabLayout; import android.support.v4.view.GravityCompat; 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.support.v7.widget.Toolbar; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.util.ArrayList; import java.util.List; import java.util.Locale; /** * Created by iwanghang on 2016/10/31. */ public class MainActivity extends AppCompatActivity{ private DrawerLayout v4_drawerLayout; private Button button_1; private Button button_2; private Button button_3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewPager v4_viewPager = (ViewPager) findViewById(R.id.v4_viewPager); TabLayout design_tabLayout = (TabLayout) findViewById(R.id.design_tabLayout); Toolbar v7_toolbar = (Toolbar) findViewById(R.id.v7_toolbar); setSupportActionBar(v7_toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); v4_drawerLayout = (DrawerLayout) findViewById(R.id.v4_drawerLayout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, v4_drawerLayout, v7_toolbar, 0, 0); v4_drawerLayout.addDrawerListener(toggle); toggle.syncState(); List<String> list = new ArrayList<>(); for (int i = 0; i < 5; i++) { list.add(String.format(Locale.CHINA,"第%02d页",i+1)); } v4_viewPager.setAdapter(new MyAdapter(getSupportFragmentManager(),list)); design_tabLayout.setupWithViewPager(v4_viewPager); NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView); initNavigation(navigationView); // 右滑菜单中的item点击事件监听 button_1 = (Button) findViewById(R.id.button_1); button_2 = (Button) findViewById(R.id.button_2); button_3 = (Button) findViewById(R.id.button_3); initButton(); // 按钮点击事件监听 } private void initButton() { button_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Snackbar.make(v,"Snackbar",Snackbar.LENGTH_LONG).show(); } }); button_2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Snackbar.make(v,"Snackbar",Snackbar.LENGTH_LONG).setAction("undo", new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "Undo", Toast.LENGTH_SHORT).show(); } }).show(); } }); button_3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Snackbar.make(v,"Snackbar",Snackbar.LENGTH_LONG).setAction("undo", new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this,CoordinatorActivity.class)); } }).show(); } }); } /** * 右滑菜单中的item点击事件监听 */ private void initNavigation(NavigationView navigationView) { navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem item) { Toast.makeText(MainActivity.this, item + " 点击 或 选中", Toast.LENGTH_SHORT).show(); switch (item.getItemId()){ case R.id.item_1: // 当点击item_1 finish(); // 关闭当前Activity 这里的效果是 退出App break; case R.id.item_2: // 当点击item_1 v4_drawerLayout.closeDrawer(GravityCompat.START); // 收起右滑菜单 break; case R.id.item_3: // 当点击item_1 v4_drawerLayout.closeDrawer(GravityCompat.START); // 收起右滑菜单 break; case R.id.item_4: // 当点击item_1 v4_drawerLayout.closeDrawer(GravityCompat.START); // 收起右滑菜单 break; } return true; } }); } }activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.iwanghang.designdemo.MainActivity" android:orientation="vertical"> <!--Toolbar为了取代actionBar出现,这里使用actionBar的高度--> <android.support.v7.widget.Toolbar android:id="@+id/v7_toolbar" android:layout_width="match_parent" android:layout_height="?actionBarSize" android:background="#006666" app:titleTextColor="#ffffff" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> <android.support.v4.widget.DrawerLayout android:id="@+id/v4_drawerLayout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.design.widget.TabLayout android:id="@+id/design_tabLayout" app:tabBackground="@color/B6B6B6" app:tabTextColor="#ffffff" app:tabSelectedTextColor="#000000" app:tabIndicatorColor="#4169E1" app:tabTextAppearance="@style/TextAppearance.AppCompat.Large" android:layout_width="match_parent" android:layout_height="wrap_content"/> <android.support.v4.view.ViewPager android:id="@+id/v4_viewPager" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"/> <Button android:id="@+id/button_1" android:text="click here button 1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button_2" android:text="click here button 2" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button_3" android:text="click here button 3" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <android.support.design.widget.NavigationView android:id="@+id/navigationView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" app:itemTextColor="@color/navigation_select" app:itemIconTint="@color/navigation_select" app:menu="@menu/navigation" app:headerLayout="@layout/header" /> </android.support.v4.widget.DrawerLayout> </LinearLayout>