Android5.0新控件实例详解

所属分类: 软件编程 / Android 阅读数: 62
收藏 0 赞 0 分享

谷歌在推出Android5.0的同时推出了一些新控件,Android5.0中最常用的新控件有下面5种。

1. CardView(卡片视图)

CardView顾名思义是卡片视图,它继承FrameLayout。它是一个带圆角的背景和阴影FrameLayout。CardView被包装为一种布局,并且经常在ListView和RecyclerView的Item布局中,作为容器使用。
CardView的使用非常简单:

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="60dp">
    <Button
      android:id="@+id/ripple_button"
      android:layout_width="match_parent"
      android:layout_height="50dp"
      android:layout_gravity="center"
      android:layout_margin="5dp"
      android:background="@drawable/ripple"
      android:gravity="center"
      android:text="我在一个CardView里面" />
  </android.support.v7.widget.CardView>

2. Patelle(调色板)

Patelle是一个辅助类,它的作用是从图片中获取突出的颜色。
它可以提取下面几种特性的突出颜色:

- Vibrant(充满活力的)
- Vibrant Dark(充满活力,黑暗的)
- Vibrant Light(充满活力的,明亮的)
- Muted(柔和的)
- Muted Dark(柔和的,黑暗的)
- Muted Light(柔和的,明亮的)

Patelle的使用也非常简单:

 // 获取应用程序图标的Bitmap
 bitmap= BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
 // 通过bitmap生成调色板palette
Palette palette=Palette.from(bitmap).generate();
 // 获取palette充满活力色颜色
 int vibrantColor=palette.getVibrantColor(Color.WHITE); 

3. Toolbar(工具栏)

Toolbar顾名思义是工具栏,作为ActionBar的替代品出现,谷歌推荐使用Toolbar替代ActionBar。
Toolbar可以放置在任何地方,不像ActionBar一样只能放置在固定的位置。
Toolbar支持比ActionBar更集中的特征。
Toolbar可能包含以下可选元素的组合:
- 导航按钮
- 品牌的Logo图像
- 标题和子标题
- 一个或多个自定义视图

this.toolbar = (Toolbar) findViewById(R.id.toolbar);
this.recyclerview = (RecyclerView) findViewById(R.id.recycler_view);
this.ripplebutton = (Button) findViewById(R.id.ripple_button);
this.button = (Button) findViewById(R.id.button);
// 设置Logo
toolbar.setLogo(R.mipmap.ic_launcher);
// 设置标题
toolbar.setTitle("Android5.0");
// 设置子标题
toolbar.setSubtitle("新控件");
//设置ActionBar,之后就可以获取ActionBar并进行操作,操作的结果就会反应在toolbar上面
setActionBar(toolbar);
//设置了返回箭头,,相当于设置了toolbar的导航按钮
getActionBar().setDisplayHomeAsUpEnabled(true);

4. RippleDrawable(波纹图)

RippleDrawable顾名思义是波纹图,只能在Android5.0以上使用,目前还没有提供RippleDrawable向下兼容的支持包。
RippleDrawable可显示一个涟漪效应响应状态变化 。

定义一个UI的背景图片为RippleDrawable
android:background="@drawable/ripple"

在drawable文件夹下面定义一个RippleDrawable的xml文件

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
  android:color="#0000FF">
  <item>
    <shape android:shape="rectangle">
      <solid android:color="#FFFFFF" />
      <corners android:radius="4dp" />
    </shape>
  </item>
</ripple> 

android:color :表示波纹的颜色

<item>:表示波纹图下面的条目

来看一下点击按钮的波纹效果

5. RecyclerView(循环视图)

RecyclerView是ListView的替代品,谷歌推荐使用RecyclerView替代ListView。

RecyclerView提供比ListView更加灵活的使用,并且性能比ListView更优。

RecyclerView可以设置线性,网格,瀑布流式三种布局管理器。

- LinearLayoutManager(线性布局管理器)
- GridLayoutManager(网格布局管理器)
- StaggeredGridLayoutManager(瀑布流式布局管理器)

注意:RecyclerView,Patelle,CardView是在单独的支持包里面,不在appcompat-v7及其依赖子包中

 要使用它们,必须导入它们的依赖包

 compile 'com.android.support:recyclerview-v7:23.1.1'
  compile 'com.android.support:palette-v7:23.1.1'
  compile 'com.android.support:cardview-v7:23.1.1' 

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多