Material Design 是 Google 推出的一种设计风格,旨在为移动应用和 Web 应用提供一种现代化、直观的设计语言。它强调简单、直接、自然的设计元素,同时又注重细节和交互效果。在 Android Studio 2.0 中,我们可以很容易地实现 Material Design,下面将介绍实现 Material Design 的 13 个步骤。
步骤一:创建新项目
在 Android Studio 中创建一个新项目,选择 Empty Activity 模板。
步骤二:导入 Material Design 库
在项目的 build.gradle 文件中添加以下代码:
dependencies { compile 'com.android.support:design:23.4.0' }
这个库包含了许多 Material Design 的组件和样式。
步骤三:修改 App 主题
在 res/values/styles.xml 文件中,将 App 主题修改为 Material Design 主题:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style>
这里的 colorPrimary、colorPrimaryDark、colorAccent 是 Material Design 中常用的颜色。
步骤四:添加 Toolbar
在布局文件中添加 Toolbar:
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />
步骤五:设置 Toolbar 为 ActionBar
在 Activity 中设置 Toolbar 为 ActionBar:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);
步骤六:添加 Floating Action Button
在布局文件中添加 Floating Action Button:
<android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email" />
步骤七:添加 SnackBar
在 Activity 中添加 SnackBar:
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } });
步骤八:添加 TabLayout
在布局文件中添加 TabLayout:
<android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabGravity="fill" app:tabMode="fixed" />
步骤九:添加 ViewPager
在布局文件中添加 ViewPager:
<android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" />
步骤十:设置 TabLayout 和 ViewPager 的关联
在 Activity 中设置 TabLayout 和 ViewPager 的关联:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager); viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager())); tabLayout.setupWithViewPager(viewPager);
步骤十一:添加 DrawerLayout
在布局文件中添加 DrawerLayout:
-- -------------------- ---- ------- --------------------------------------- ------------------------------- ----------------------------------- ------------------------------------- ------------- ----------------------------------- ------------------------------------ ------------------------------- ---------------------------------- ------------------------- ----------------------------------- ------------------------------------------- --------------------------------------- --------------------------------------------- -- ------------ ------------------------------- ----------------------------------- ------------------------------------ -- --------------- --------------------------------------------- -------------------------- ----------------------------------- ------------------------------------ ------------------------------ ------------------------------------- ------------------------- -- -----------------------------------------
步骤十二:设置 DrawerLayout 和 Toolbar 的关联
在 Activity 中设置 DrawerLayout 和 Toolbar 的关联:
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawerLayout.addDrawerListener(toggle); toggle.syncState();
步骤十三:添加 Bottom Navigation
在布局文件中添加 Bottom Navigation:
<android.support.design.widget.BottomNavigationView android:id="@+id/bottom_navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" app:menu="@menu/bottom_navigation_menu" />
这里的 bottom_navigation_menu 是自定义的菜单文件。
至此,我们已经完成了在 Android Studio 2.0 中实现 Material Design 的 13 个步骤。这些步骤涵盖了 Material Design 中常用的组件和样式,可以让我们快速地搭建出一个现代化、直观的应用程序。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/67d3a7c1a941bf71346faa5c