Android Material风格的应用(一)--AppBar TabLayout
打造Material风格的Android应用
Android Material风格的应用(一)--AppBar TabLayout
Android Material风格的应用(二)--RecyclerView
Android Material风格的应用(三)--DrawerLayout
Android Material风格的应用(四)--FloatActionButton
Android Material风格的应用(五)--CollapsingToolbar
开发环境:
- JDK 8+
- Android Studio 2.2.2
工程源码
Themes And Colors
主题颜色值 res/values/colors.xml
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
如此,添加一个Light主题风格的style res/values/styles.xml
<style name="AppThemeBase" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
使用 parent="Theme.AppCompat.Light.NoActionBar"
是在应用中去除ActionBar
运行效果图
布局和动画

添加 Toolbar
在res/layout/activity_main.xml
和MainActivity.java
中添加toolbar,这里使用CoordinatorLayout
作为一个容器
里面会包含TabLayout FloatingActionButton Toolbar
等,引入design的支持库compile 'com.android.support:design:24.2.1'
activity_main.xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_content"> <android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/app_bar_layout"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="@+id/toolbar"
app:layout_scrollFlags="enterAlways|scroll"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"></android.support.v7.widget.Toolbar> <android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabs">
</android.support.design.widget.TabLayout>
</TableLayout>
</android.support.design.widget.AppBarLayout> </android.support.design.widget.CoordinatorLayout>MainActivity.java
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);添加TabLayout
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabs);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));添加 Fragment 和 ViewPager
在activity_main.xml
中添加ViewPager
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewpager"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v4.view.ViewPager>通过添加ViewPager和TabLayout联动,创建3个Fragment的类
ListContentFragment.java
TileContentFragment.java
和CardContentFragment.java
添加
item_list.xml
item_tile.xml
item_card.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List"/>
</LinearLayout>分别创建不同的Fragment文件和布局对应
ListContentFragment.java
public class ListContentFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.item_list,null);
return v;
}
}TileContentFragment.java
public class TileContentFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.item_tile,null);
}
}CardContentFragment.java
public class CardContentFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.item_card,null);
}
}在
MainActivity.java
中添加ViewPager
ViewPager viewPager = (ViewPager)findViewById(R.id.viewpager);
setupViewPager(viewPager); private void setupViewPager(ViewPager viewPager){
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(new ListContentFragment(),"List");
adapter.addFragment(new TileContentFragment(),"Tile");
adapter.addFragment(new CardContentFragment(),"Card");
viewPager.setAdapter(adapter);
} static class Adapter extends FragmentPagerAdapter{
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitle = new ArrayList<>();
public Adapter(FragmentManager fm) {
super(fm);
} @Override
public Fragment getItem(int position) {
return fragmentList.get(position);
} @Override
public int getCount() {
return fragmentList.size();
} public void addFragment(Fragment fragment,String title){
fragmentList.add(fragment);
fragmentTitle.add(title);
} @Override
public CharSequence getPageTitle(int position) {
return fragmentTitle.get(position);
}
}
Android Material风格的应用(一)--AppBar TabLayout的更多相关文章
- Android Material风格的应用(五)--CollapsingToolbar
Collapsing Toolbar Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--RecyclerViewA ...
- Android Material风格的应用(四)--FloatActionButton
添加 FloatActionButton和SnackBar Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--Re ...
- Android Material风格的应用(二)--RecyclerView
添加RecyclerView Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--RecyclerViewAndro ...
- Android Material风格的应用(三)--DrawerLayout
添加抽屉导航 Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--RecyclerViewAndroid Mater ...
- android ------ AndroidX的 Tablayout(com.google.android.material.tabs.TabLayout) 的使用
前面呢,有写过TabLayout的博客,最近开发用到了AndroidX来解决前面的问题,不要工具类设置下划线的问题了,来总结一下 Android--------TabLayout实现新闻客户端顶部导航 ...
- Android Material Design 兼容库的使用
Android Material Design 兼容库的使用 mecury 前言:近来学习了Android Material Design 兼容库,为了把这个弄懂,才有了这篇博客,这里先推荐两篇博客: ...
- Android Material Design之Toolbar与Palette
转:http://blog.csdn.net/jdsjlzx/article/details/41441083 前言 我们都知道Marterial Design是Google推出的全新UI设计规范,如 ...
- android Material Design详解
原文地址:http://blog.csdn.net/jdsjlzx/article/details/41441083/ 前言 我们都知道Marterial Design是Google推出的全新UI设计 ...
- Android Material Design控件学习(三)——使用TextInputLayout实现酷市场登录效果
前言 前两次,我们学习了 Android Material Design控件学习(一)--TabLayout的用法 Android Material Design控件学习(二)--Navigation ...
随机推荐
- Centos6.5 安装lamp环境
转载自:http://www.jb51.net/article/37987.htm (转载请注明出处,谢谢) 准备篇: 1.配置防火墙,开启80端口.3306端口vi /etc/sysconfig/i ...
- Spring : 征服数据库(一)
严格的说.这里征服的是关系型数据库.之后笔者会以MongoDB为例,给出非关系型数据库的解决方式,敬请期待. 获取连接,操作,关闭,不知所云的异常...是的,你受够了.在使用纯JDBC时你訪问数据库时 ...
- React Native入门——IDE及其它相关基础技术
关于React Native的开发,当中一个问题是缺少好用的IDE,有些人说不就是JS么,搞一个记事本也就写了,那样尽管牛逼,但事实上还是非常头大的,有一款好的IDE还是能提升开发效率的,这里对几个还 ...
- HDU 2846 Repository (字典树 后缀建树)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- Spring MVC : Java模板引擎 Thymeleaf (三)
以下以构造一个表单開始,解说 Thymeleaf的使用方法. 为了演示方便,还是以经典的注冊为例. 这是Thymeleaf的form的形式, <form action="#" ...
- openstack-dashboard开发环境搭建
1,在开发过程中,一般都要,将dashboard这个组件单独执行在自己的本地的linux系统中(Ubuntu或centos),那个比較顺手用哪个.假设不习惯,能够用vmwareworkstation安 ...
- OpenCASCADE7.3.0 is available for download
OpenCASCADE7.3.0 is available for download OPEN CASCADE is pleased to announce a new public release ...
- AVEVA PDMS to 3ds Max - RvmTranslator6.0beta
AVEVA PDMS to 3ds Max - RvmTranslator6.0beta eryar@163.com RvmTranslato6.0 translate PDMS RVM to 3ds ...
- ajax模仿iframe
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- ASP.NET Web.config学习
花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <? ...