打造Material风格的Android应用

Android Material风格的应用(一)--AppBar TabLayout
Android Material风格的应用(二)--RecyclerView
Android Material风格的应用(三)--DrawerLayout
Android Material风格的应用(四)--FloatActionButton
Android Material风格的应用(五)--CollapsingToolbar

开发环境:

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运行效果图

布局和动画

TabLayout
  • 添加 Toolbar
    res/layout/activity_main.xmlMainActivity.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.javaCardContentFragment.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的更多相关文章

  1. Android Material风格的应用(五)--CollapsingToolbar

    Collapsing Toolbar Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--RecyclerViewA ...

  2. Android Material风格的应用(四)--FloatActionButton

    添加 FloatActionButton和SnackBar Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--Re ...

  3. Android Material风格的应用(二)--RecyclerView

    添加RecyclerView Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--RecyclerViewAndro ...

  4. Android Material风格的应用(三)--DrawerLayout

    添加抽屉导航 Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--RecyclerViewAndroid Mater ...

  5. android ------ AndroidX的 Tablayout(com.google.android.material.tabs.TabLayout) 的使用

    前面呢,有写过TabLayout的博客,最近开发用到了AndroidX来解决前面的问题,不要工具类设置下划线的问题了,来总结一下 Android--------TabLayout实现新闻客户端顶部导航 ...

  6. Android Material Design 兼容库的使用

    Android Material Design 兼容库的使用 mecury 前言:近来学习了Android Material Design 兼容库,为了把这个弄懂,才有了这篇博客,这里先推荐两篇博客: ...

  7. Android Material Design之Toolbar与Palette

    转:http://blog.csdn.net/jdsjlzx/article/details/41441083 前言 我们都知道Marterial Design是Google推出的全新UI设计规范,如 ...

  8. android Material Design详解

    原文地址:http://blog.csdn.net/jdsjlzx/article/details/41441083/ 前言 我们都知道Marterial Design是Google推出的全新UI设计 ...

  9. Android Material Design控件学习(三)——使用TextInputLayout实现酷市场登录效果

    前言 前两次,我们学习了 Android Material Design控件学习(一)--TabLayout的用法 Android Material Design控件学习(二)--Navigation ...

随机推荐

  1. codevs——T1006 等差数列

    http://codevs.cn/problem/1006/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Descr ...

  2. RvmTranslator6.0 - Dassault Systemes 3DXML

    RvmTranslator6.0 - Dassault Systemes 3DXML eryar@163.com 1. Introduction RvmTranslator can translate ...

  3. 将Firefox设置为使用远程DNS

    将Firefox设置为使用远程DNS 原文 https://www.my-proxy.com/blog/firefox-remote-dns 测试当前在用DNS              https: ...

  4. jquery15 on() trigger() : 事件操作的相关方法

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  5. js对数组进行操作

    1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限, ...

  6. 上市公司恋上互联网金融 目前已有14家涌入P2P

    时至今日,互联网金融已蔚然成风,诸多上市公司正前赴后继介入到P2P业务中,据记者初步统计,目前至少有14家A股上市公司参与了P2P业务.央行6月份的报告显示,中国当前有600多家P2P公司,交易额达到 ...

  7. eclipse工作空间配置导出

    由于工作与学习的需求,需要使用不同的工作空间.而eclipse的新建工作空间其他以前的配置都没有继承过来,那么就得重新配置一遍. 经过学习其他前辈们的经验与自己的摸索总结一下3种方法: 方法一:使用e ...

  8. iframe自适应高度解决方法 .

    <div id="leftBar"> <iframe name="tag" src="_iframe.html" styl ...

  9. 【习题 8-3 UVA - 12545】Bits Equalizer

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果1的个数第一个串比第2个串多. 那么就无解. 否则. 找几个位置去凑1 优先找'?'然后才是0的位置 剩余的全都用swap操作就 ...

  10. 关于bat的变量赋值和解析机制

    以下的演示涉及几个知识点: 1. 怎样把命令输出内容保存到变量中? 2. 多次改变变量值,为什么在for或是if的()中的无效,怎样变通? 3. bat的function实现? 见代码,和代码凝视 : ...