打造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. 及格的产品vs优秀的产品

    类似的产品,做了同样的一个功能,但是,我们还是可以很明显的感受到不同,这种不同我们常常把他叫做「用户体验」. 来看看2组类似产品相似功能的设计: 识别到歌名vs还可以滚动展示歌词 在很早的时候就存在一 ...

  2. python + eclipse + django + postgresql 开发网站(一)

    一.配置开发环境 1.安装Python 载地址下:http://www.python.org/getit/

  3. 考满分软件测试工程师(实习)面试&软达启航面试

    考满分软件测试工程师(实习)面试 从这学期秋季开学的时候开始准备找工作,一边学习看书,一边完善简历海投:九月下旬的时候在年级实习群里看到考满分发的宣传海报马上就加了hr的微信,hr要了我的简历,并给技 ...

  4. 5.brackets 快捷键 有大用

    转自:https://blog.csdn.net/u012011360/article/details/41209223 ctrl+b 当选中一个文本时,会出现相同的文本,被高亮显示 按ctrl+b ...

  5. Linux LiveCD 诞生记

    Linux LiveCD 诞生记 650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic ...

  6. MarkDown study:

    #MarkDown study:## 区块元素:### 段落和换行 段落:由一个或多个连续的文本行组成,它的前后要有一个以上的空行(空行的定义是显示上看起来像是空的,便会被视为空行.比方说,若某一行只 ...

  7. hostname---显示和设置系统的主机

    hostname命令用于显示和设置系统的主机名称.环境变量HOSTNAME也保存了当前的主机名.在使用hostname命令设置主机名后,系统并不会永久保存新的主机名,重新启动机器之后还是原来的主机名. ...

  8. 今日 SGU 5.6

    SGU 106 题意:问你有多少个<x,y>,满足ax+by+c=0,x1<=x<=x2,y1<=y<=y2 收货:拓展欧几里得求解的是这种方程,ax+by=1,g ...

  9. 洛谷 P1801 黑匣子_NOI导刊2010提高(06)(未完)

    P1801 黑匣子_NOI导刊2010提高(06) 题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个 ...

  10. [RxJS] Marbles Testings

    Install: npm install — save-dev jasmine-marbles Basic example: import {cold, getTestScheduler} from ...