分类: Android UI2015-06-15 16:44 1145人阅读 评论(5) 收藏 举报
 
 

【转载请注明出处:http://blog.csdn.net/feiduclear_up/article/details/46500865 CSDN 废墟的树】

在前不久的谷歌2015 I/O大会上,发布了Android新版本M,貌似从这个版本开始Android不在以数字命名版本了。

在这次的I/O大会上谷歌对Android并没有很大的改变,主要是修改完善之前Android L版本。不过在谷歌推出

Material Design设计风格之后,还是做了很多风格上的兼容,比如v7包的 RecyclerView,CardView,Palette等

这次的I/O大会上也继续完善了MD设计支持库,这次谷歌推出了Android Design Support Library 库,全面支持

MD设计风格的UI效果。Design Support Library库吸收了8 个新的 material design 组件!最低支持 Android

2.1,其实很多组件都是Github上比较火的,只是谷歌把它官方化了,便于开发者使用。今天我们来学习

FloatingActionButton,TextInputLayout,Snackbar,TabLayout 四种控件。

前提

为了能使用 这些 material design 组件,你需要去更新最新的SDK中的Extras支持库,如下图:

ps:在天朝上国,这种更新是需要翻墙的或者使用代理的,大家自行想办法。

更新完之后,在build.gralde文件下引入如下包:

  1. compile 'com.android.support:design:22.2.0'
  • 1

现在,我们可以开始使用Material Design组件啦!来看看新组件有什么特别的地方吧!

FloatingActionButton

顾名思义:这是一个浮动按钮。先上效果图啦!ps:没有效果图的UI博客很蛋疼的。

以上是三种不同效果的FloatingActionButton。XML布局代码如下:

  1. <android.support.design.widget.FloatingActionButton
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:src="@drawable/ic_discuss"
  5. />
  • 1
  • 2
  • 3
  • 4
  • 5

由于FloatingActionButton是重写ImageView的,所有FloatingActionButton拥有ImageView的一切属性。为了

控制FloatingActionButton的大小,背景颜色,阴影的深度等,我们可以通过如下属性来控制这些效果:

  1. app:fabSize :FloatingActionButton的大小,有两种赋值分别是 “mini” 和 “normal”,默认是“normal”.
  2. app:backgroundTint:FloatingActionButton的背景颜色,默认的背景颜色是Theme主题中的
  1. <item name="colorAccent">#ff0000</item>
  • 1

颜色,不了解的童鞋们可以参考Android5.x新特性之 Toolbar和Theme的使用:http://blog.csdn.net/feiduclear_up/article/details/46457433。 
3. app:elevation :FloatingActionButton阴影的深度,默认是有阴影的,如果觉得默认阴影深度有点大,可以改变这个属性来修改阴影深度。

上面三个效果图的XML布局代码如下:

  1. <LinearLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="horizontal">
  5. <android.support.design.widget.FloatingActionButton
  6. android:id="@+id/btn"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:src="@drawable/ic_discuss"
  10. app:fabSize="mini" />
  11. <android.support.design.widget.FloatingActionButton
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:src="@drawable/ic_discuss"
  15. />
  16. <android.support.design.widget.FloatingActionButton
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:src="@drawable/ic_discuss"
  20. app:backgroundTint="#000af4"
  21. app:fabSize="normal"
  22. app:elevation="1dp"
  23. />
  24. </LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

注意点

  1. 不能通过 android:background 属性来改变 FloatingActionButton的背景颜色,只能通过app:backgroundTint属性改变,因为FloatingActionButton是继承自ImageView的。

TextInputLayout

该控件是用于EditView输入框的,主要解决之前EditView在获得焦点编辑时hint属性提示语消失,这一点在一个页

面有多个EditView输入框的时候不是很好,因为很有可能用户在输入多个EditView之后,不知道当前EditView需

要输入什么内容。为了解决这一问题,TextInputLayout就此诞生了。TextInputLayout是继承自LinearLayout容

器布局,因此我们需要将EditView包含在TextInputLayout之内才可以使用,言外之意:TextInputLayout不能单

独使用。废话不多说,先上效果图啊:

XML布局代码如下:

  1. <android.support.design.widget.TextInputLayout
  2. android:id="@+id/textInput"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content">
  5. <EditText
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:textColor="@android:color/black"/>
  9. </android.support.design.widget.TextInputLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

代码也可以看出TextInputLayout包裹着EditView。

为了达到以上效果,我们还需添加如下代码:

  1. final TextInputLayout inputLayout = findView(R.id.textInput);
  2. inputLayout.setHint("请输入姓名:");
  3. EditText editText = inputLayout.getEditText();
  4. editText.addTextChangedListener(new TextWatcher() {
  5. @Override
  6. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  7. }
  8. @Override
  9. public void onTextChanged(CharSequence s, int start, int before, int count) {
  10. if (s.length()>4){
  11. inputLayout.setErrorEnabled(true);
  12. inputLayout.setError("姓名长度不能超过4个");
  13. }else{
  14. inputLayout.setErrorEnabled(false);
  15. }
  16. }
  17. @Override
  18. public void afterTextChanged(Editable s) {
  19. }
  20. });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

TextInputLayout 不仅能让EditView的提示语上弹显示在EditView之上,而且还能把错误信息显示在EditView之下。

TextInputLayout常用的方法有如下:

  1. setHint():设置提示语。
  2. getEditText():得到TextInputLayout中的EditView控件。
  3. setErrorEnabled():设置是否可以显示错误信息。
  4. setError():设置当用户输入错误时弹出的错误信息。

注意点

  1. TextInputLayout不能单独使用,需要包裹EditView组件。

【转载请注明出处:http://blog.csdn.net/feiduclear_up/article/details/46500865 CSDN 废墟的树】

Snackbar的使用

Snackbar提供了一个介于Toast和AlertDialog之间轻量级控件,它可以很方便的提供消息的提示和动作反馈。

废话不少说,妹子,上图: 

Snackbar的使用和Toast很类似,调用代码如下:

  1. final Snackbar snackbar = Snackbar.make(inputLayout,"测试弹出提示",Snackbar.LENGTH_LONG);
  2. snackbar.show();
  3. snackbar.setAction("取消",new View.OnClickListener() {
  4. @Override
  5. public void onClick(View v) {
  6. snackbar.dismiss();
  7. }
  8. });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

第一个参数View 可以是当前父布局中的任何一个view对象都可以。之后的参数和Toast参数一样。Snackbar可以

设置Action行为事件,使用的方法是public Snackbar setAction (CharSequence text, View.OnClickListener listener); 也可以为Snackbar设置多个Action行为事件。Action的字体颜色默认使用系统主题中的如下颜色

  1. <item name="colorAccent">#ff0000</item>
  • 1

当然你可以通过代码去改变Action的字体颜色:Snackbar setActionTextColor (int color);

注意

  1. Snackbar可以同时设置多个Action行为事件
  2. Snackbar是从整个界面的底部弹出。

TabLayout

Tabs选项卡,效果类似网易新闻客户端的Tab。其实实现Tabs选项卡的效果有很多中方法,Github上也有很多好

用的开源控件,只是这次谷歌把它官方化了,使得开发者无需引用第三方库,就能方便的使用。效果图:

XML布局如下:

  1. <android.support.design.widget.TabLayout
  2. android:id="@+id/tabs"
  3. <!--Tab被选中字体的颜色-->
  4. app:tabSelectedTextColor="@android:color/holo_blue_bright"
  5. <!--Tab未被选中字体的颜色-->
  6. app:tabTextColor="@android:color/black"
  7. <!--Tab指示器下标的颜色-->
  8. app:tabIndicatorColor="@android:color/holo_blue_bright"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

常用的属性有三个:

  1. app:tabSelectedTextColor:Tab被选中字体的颜色
  2. app:tabTextColor:Tab未被选中字体的颜色
  3. app:tabIndicatorColor:Tab指示器下标的颜色

TabLayout常用的方法如下: 
- addTab(TabLayout.Tab tab, int position, boolean setSelected) 增加选项卡到 layout 中 
- addTab(TabLayout.Tab tab, boolean setSelected) 同上 
- addTab(TabLayout.Tab tab) 同上 
- getTabAt(int index) 得到选项卡 
- getTabCount() 得到选项卡的总个数 
- getTabGravity() 得到 tab 的 Gravity 
- getTabMode() 得到 tab 的模式 
- getTabTextColors() 得到 tab 中文本的颜色 
- newTab() 新建个 tab 
- removeAllTabs() 移除所有的 tab 
- removeTab(TabLayout.Tab tab) 移除指定的 tab 
- removeTabAt(int position) 移除指定位置的 tab 
- setOnTabSelectedListener(TabLayout.OnTabSelectedListener onTabSelectedListener) 为每个 tab 增加选择监听器 
- setScrollPosition(int position, float positionOffset, boolean updateSelectedText) 设置滚动位置 
- setTabGravity(int gravity) 设置 Gravity 
- setTabMode(int mode) 设置 Mode,有两种值:TabLayout.MODE_SCROLLABLE和TabLayout.MODE_FIXED分别表示当tab的内容超过屏幕宽度是否支持横向水平滑动,第一种支持滑动,第二种不支持,默认不支持水平滑动。 
- setTabTextColors(ColorStateList textColor) 设置 tab 中文本的颜色 
- setTabTextColors(int normalColor, int selectedColor) 设置 tab 中文本的颜色 默认 选中 
- setTabsFromPagerAdapter(PagerAdapter adapter) 设置 PagerAdapter 
- setupWithViewPager(ViewPager viewPager) 和 ViewPager 联动

一般TabLayout都是和ViewPager共同使用才发挥它的优势,现在我们通过代码来看看以上方法的使用。

  1. viewPager = findView(R.id.viewPager);
  2. tabLayout = findView(R.id.tabs);
  3. List<String> tabList = new ArrayList<>();
  4. tabList.add("Tab1");
  5. tabList.add("Tab2");
  6. tabList.add("Tab3");
  7. tabLayout.setTabMode(TabLayout.MODE_FIXED);//设置tab模式,当前为系统默认模式
  8. tabLayout.addTab(tabLayout.newTab().setText(tabList.get(0)));//添加tab选项卡
  9. tabLayout.addTab(tabLayout.newTab().setText(tabList.get(1)));
  10. tabLayout.addTab(tabLayout.newTab().setText(tabList.get(2)));
  11. List<Fragment> fragmentList = new ArrayList<>();
  12. for (int i = 0; i < tabList.size(); i++) {
  13. Fragment f1 = new TabFragment();
  14. Bundle bundle = new Bundle();
  15. bundle.putString("content", "http://blog.csdn.net/feiduclear_up \n CSDN 废墟的树");
  16. f1.setArguments(bundle);
  17. fragmentList.add(f1);
  18. }
  19. TabFragmentAdapter fragmentAdapter = new TabFragmentAdapter(getSupportFragmentManager(), fragmentList, tabList);
  20. viewPager.setAdapter(fragmentAdapter);//给ViewPager设置适配器
  21. tabLayout.setupWithViewPager(viewPager);//将TabLayout和ViewPager关联起来。
  22. tabLayout.setTabsFromPagerAdapter(fragmentAdapter);//给Tabs设置适配器
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

就不解释了,都有注释,来看看以上代码的TabFragmentAdapter和TabFragment实现如下:

TabFragmentAdapter

  1. public class TabFragmentAdapter extends FragmentStatePagerAdapter {
  2. private List<Fragment> mFragments;
  3. private List<String> mTitles;
  4. public TabFragmentAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles) {
  5. super(fm);
  6. mFragments = fragments;
  7. mTitles = titles;
  8. }
  9. @Override
  10. public Fragment getItem(int position) {
  11. return mFragments.get(position);
  12. }
  13. @Override
  14. public int getCount() {
  15. return mFragments.size();
  16. }
  17. @Override
  18. public CharSequence getPageTitle(int position) {
  19. return mTitles.get(position);
  20. }
  21. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

TabFragment

  1. public class TabFragment extends Fragment {
  2. private String content;
  3. private View view;
  4. @Override
  5. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  6. view = inflater.inflate(R.layout.item, container,false);
  7. return view;
  8. }
  9. @Override
  10. public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  11. super.onActivityCreated(savedInstanceState);
  12. content = getArguments().getString("content");
  13. TextView tvContent = (TextView) view.findViewById(R.id.tv_tab_content);
  14. tvContent.setText(content + "");
  15. }
  16. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

注意 :有这么一种情况,当Tabs中的内容超过了手机屏幕的宽度时,Tabs选项卡中的tab为什么不支持水平滑动?其实TabLayout是支持水平滑动的,只需要你在代码中添加如下一行即可:

  1. tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);//设置tab模式
  • 1

限于篇幅有点长,接下来的CoordinatorLayout , CollapsingToolbarLayout 和 AppBarLayout,NavigationView将

在下一篇博客学习。以上代码,如有疑问,欢迎共同讨论。

源码地址 https://github.com/xujinping/AndroidDesignLibrary/tree/master

【转】Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用的更多相关文章

  1. 【转】Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用

    Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用 分类: Android UI ...

  2. Android其它新控件 (转)

    原文出处:http://blog.csdn.net/lavor_zl/article/details/51312715 Android其它新控件是指非Android大版本更新时提出的新控件,也非谷歌I ...

  3. android design 新控件

    转载请标明出处: http://blog.csdn.net/forezp/article/details/51873137 本文出自方志朋的博客 最近在研究android 开发的新控件,包括drawe ...

  4. [转 载] android 谷歌 新控件(约束控件 )ConstraintLayout 扁平化布局

    序 在Google IO大会中不仅仅带来了Android Studio 2.2预览版,同时带给我们一个依赖约束的库. 简单来说,她是相对布局的升级版本,但是区别与相对布局更加强调约束.何为约束,即控件 ...

  5. Android L新控件RecyclerView简介

    Android L是android进化史上的里程碑,尽管还没有正式发布4.5或者5.0,但预览版也同样精彩. 这篇文章只是另外一篇博客的总结性翻译,能够读懂原文的,可以点开这个链接去阅读精彩的原文:h ...

  6. android L 新控件侧滑菜单DrawerLayout 使用教程

    介绍 drawerLayout是Support Library包中实现了侧滑菜单效果的控件,可以说drawerLayout是因为第三方控件如MenuDrawer等的出现之后,google借鉴而出现的产 ...

  7. Android Material Design控件学习(一)——TabLayout的用法

    前言 Google官方在14年Google I/O上推出了全新的设计语言--Material Design.一并推出了一系列实现Material Design效果的控件库--Android Desig ...

  8. android L新控件RecyclerView详解与DeMo[转]

    http://blog.csdn.net/codebob/article/details/37813801 在谷歌的官网我们可以看到它是这样介绍的: RecyclerView  is a more a ...

  9. android L新控件RecyclerView具体解释DeMo

    简介 在谷歌的官方网站上,我们可以看到,它是此演示文稿:RecyclerView is a more advanced and flexible version of ListView. This w ...

随机推荐

  1. NOIP200002税收与补贴

    试题描述 每样商品的价格越低,其销量就会相应增大.现已知某种商品的成本及其在若干价位上的销量(产品不会低于成本销售),并假设相邻价位间销量的变化是线性的且在价格高于给定的最高价位后,销量以某固定数值递 ...

  2. Checkbox的选中删除功能且Ajax返回后清除所选行

    转摘:http://javaweb1024.com/qianduan/jQuery/2015/04/13/544.html 功能描述:多选框勾选以后(全部或者部分),需要想后台提交已勾选的数据(Aja ...

  3. unauthenticated user reading from net

    今天有台数据库异常,登录服务器后执行show processlist,发现大量的 unauthenticated user 状态.如下: 于是第一时间想到DNS反向解析的问题,于是看看是否关闭DNS解 ...

  4. border 变形计

    趣味实现楼阁或者展示板的画面 <!DOCTYPE HTML> <html lang="en-US"> <head> <meta chars ...

  5. Phaser提供了Button对象简单的实现一个按钮

    Phaser是一个简单易用且功能强大的html5游戏框架,利用它可以很轻松的开发出一个html5游戏.在这篇文章中我就教大家如何用Phaser来制作一个前段时间很火爆的游戏:Flappy Bird,希 ...

  6. windows实时操作系统

    最近一个项目需要用windows进行实时定时操作以实现同步功能(12ms),不过由于windows是分时系统,其可供用户使用的定时器误差较大. 通过查找发现了一个ardence公司开发的一个叫做RTX ...

  7. debug阶段工作期站立会议2(进度推进)

    组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...

  8. linux使脚本在后台运行

    一.为什么要使程序在后台执行 我们计算的程序都是周期很长的,通常要几个小时甚至一个星期.我们用的环境是用putty远程连接到日本Linux服务器.所以使程序在后台跑有以下三个好处: 1:我们这边是否关 ...

  9. Android开发的七大环节

    Android开发的七大环节   浏览:25 发布日期:2015/10/27 分类:职场感悟 一个完整的Android 开发流程主要包括策划.软件.交互.视觉.测试.运营维护这七大环节,其中的每一个环 ...

  10. NERDTree 快捷键辑录

    参看连接:http://www.cnblogs.com/lexus/archive/2012/11/04/2753187.html http://yang3wei.github.io/blog/201 ...