Android5.0新控件实例详解

作者:ganchuanpu 时间:2022-12-06 21:58:18 

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

Android5.0新控件实例详解

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>

Android5.0新控件实例详解

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);

Android5.0新控件实例详解

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>:表示波纹图下面的条目

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

Android5.0新控件实例详解

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'

来源:https://www.cnblogs.com/ganchuanpu/archive/2018/01/31/8394852.html

标签:android,5.0,新控件
0
投稿

猜你喜欢

  • Android性能优化全局异常处理详情

    2023-12-26 16:27:27
  • maven setting.xml文件配置禅定之旅

    2023-01-08 12:07:19
  • C#中的随机数函数Random()

    2022-05-11 08:07:15
  • 鉴权认证+aop+注解+过滤feign请求的实例

    2022-06-05 14:25:34
  • Android中的设计模式

    2022-09-13 06:33:21
  • C#图像处理之头发检测的方法

    2023-11-08 22:10:28
  • C#中闭包概念讲解

    2022-08-16 05:16:28
  • Spring boot2+jpa+thymeleaf实现增删改查

    2021-06-02 07:21:49
  • C# char类型字符转换大小写的实现代码

    2021-07-27 19:21:09
  • Netty分布式高性能工具类FastThreadLocal和Recycler分析

    2023-03-20 09:13:09
  • Hadoop源码分析六启动文件namenode原理详解

    2021-08-20 01:03:55
  • Springboot使用test无法启动问题的解决

    2021-07-03 22:09:23
  • 一个简单的Spring容器初始化流程详解

    2023-11-13 02:10:55
  • Android编程中出现The connection to adb is down问题的解决方法

    2022-06-27 17:46:07
  • SpringBoot集成vue的开发解决方案

    2023-11-24 20:58:10
  • Java Web使用简单的批处理操作(记事本+Tomcat)

    2021-08-13 10:08:33
  • 两种用空格分隔的java字符串的方式

    2023-10-01 05:51:04
  • 在多线程中调用winform窗体控件的实现方法

    2023-09-13 09:07:43
  • Java的可变参数与Collections类的功能示例解析

    2022-03-05 18:45:47
  • Android实现图片点击放大

    2023-03-19 17:40:59
  • asp之家 软件编程 m.aspxhome.com