Android5.0(lollipop)新特性介绍(一)

今年6月的Google I/O大会上,Android L的初次见面我相信让会让很多android粉丝有些小激动和小期待,当然作为开发者的我来说,激动不言而喻,毕竟这是自08年以来改变最大的一个版本。新的设计语言(Material Design),5000多个新增api。废话不多说,今天要说的基本都是在Android5.0中很常见,也算是对自己学习的一种记录。

1.CardView

顾名思义,CardView 卡片视图,继承自framelayout,可以通过设置圆角以及阴影来展示带有像卡片一样的效果

用法:在布局声明CardView(注意:需要引入android-support-v7-cardview.jar)

<span style="font-size:14px;"><android.support.v7.widget.CardView
            android:id="@+id/cardview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:elevation="100dp"
            card_view:cardBackgroundColor="@color/cardview_initial_background"
            card_view:cardCornerRadius="8dp"
            android:layout_marginLeft="@dimen/margin_large"
            android:layout_marginRight="@dimen/margin_large">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/margin_medium"
                android:text="@string/cardview_contents"/>
</android.support.v7.widget.CardView></span>

在Activity中通过调用mCardView.setRadius(progress)与mCardView.setElevation(progress)方法来分别设置CardView的圆角和阴影。

2.RecyclerView

RecyclerView的出现可以替代ListView和GridView,它标准化了ViewHolder,之前我们在写ListView的Adapter时需要自定义ViewHolder来提升ListView速度。使用RecyclerView的时候必须要设置LayoutManager,它会通知系统以什么样的布局来展示RecyclerView,目前系统提供了2种LayoutManager,LinearLayoutManager和GridLayoutManager对应着线性和格子,当然了也可以自定义LayoutManager来满足各种需求

用法:布局文件中声明RecyclerView(注意:需要引入android-support-v7-recyclerview.jar)

<span style="font-size:14px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:checkedButton="@+id/linear_layout_rb">
        <RadioButton android:id="@+id/linear_layout_rb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/linear_layout_manager"/>
        <RadioButton android:id="@+id/grid_layout_rb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/grid_layout_manager"/>
    </RadioGroup>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout></span>

在Activity中调用如下代码

<span style="font-size:14px;">mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
//类似listview
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//类似grdiview
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
mAdapter = new CustomAdapter(mDataset);
// Set CustomAdapter as the adapter for RecyclerView.
mRecyclerView.setAdapter(mAdapter);</span>

CustomAdapter代码如下

<span style="font-size:14px;">public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
    private static final String TAG = "CustomAdapter";

    private String[] mDataSet;

    /**
     * Provide a reference to the type of views that you are using (custom ViewHolder)
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView textView;

        public ViewHolder(View v) {
            super(v);
            // Define click listener for the ViewHolder's View.
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
            textView = (TextView) v.findViewById(R.id.textView);
        }

        public TextView getTextView() {
            return textView;
        }
    }

    /**
     * Initialize the dataset of the Adapter.
     *
     * @param dataSet String[] containing the data to populate views to be used by RecyclerView.
     */
    public CustomAdapter(String[] dataSet) {
        mDataSet = dataSet;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        // Create a new view.
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.text_row_item, viewGroup, false);

        return new ViewHolder(v);
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        // Get element from your dataset at this position and replace the contents of the view
        // with that element
        viewHolder.getTextView().setText(mDataSet[position]);
        viewHolder.getTextView().setPadding(0, position, 0, 0);
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataSet.length;
    }
}</span>

今天先介绍到这里,后续还会介绍FloatActionBar,触摸反馈,Activity过场动画等。

转载请标注出处,多谢~

时间: 2024-08-04 02:46:19

Android5.0(lollipop)新特性介绍(一)的相关文章

OpenNebula 4.0 Beta 新特性介绍

http://blog.chinaunix.net/uid-20940095-id-3561376.html 2013年3月26号,OpenNebula社区发布了OpenNebula 4.0 Beta,与OpenNebula 3.8相比,此版本增加了很多新的特性,同时也解决了之前版本的一些bug,下面分组件总结一下OpenNebula 4.0的new features and bug fix. 一.OpenNebula Core的新特性1. VM System Snapshots   虚拟机系统

Hadoop 2.4.0新特性介绍

在2014年4月7日,Apache发布了Hadoop 2.4.0 .相比于hadoop 2.3.0,这个版本有了一定的改进,突出的变化可以总结为下列几点(官方文档说明): 1 支持HDFS访问控制列表(ACL,Access Control Lists) 这个特性解决了在一定情况下,文件权限访问的权限问题.其机制是基于Linux文件访问权限的特征,如果你熟悉Linux的文件访问机制,你就不用再去理解HDFS文件访问的特性了. 有了ACL特性后,对HDFS文件系统就具有了良性的扩展特性.HDFS-4

NHibernate 3.0.0.Alpha1 发布及新特性介绍

发布 刚刚NHibernate的Leader--Fabio Maulo发布了NHibernate 3.0.0.Alpha1版本,这是NHibernate 3.0.0的第一个公开测试版本. 下载地址 你可以到这里下载NHibernate 3.0.0.Alpha1,基于.Net3.5平台,具体文件如下. NHibernate源码:NHibernate-3.0.0.Alpha1-src.zip NHibernate二进制文件:NHibernate-3.0.0.Alpha1-bin.zip 特性介绍 N

相比于python2.6,python3.0的新特性。

这篇文章主要介绍了相比于python2.6,python3.0的新特性.更详细的介绍请参见python3.0的文档. Common Stumbling Blocks 本段简单的列出容易使人出错的变动. print语句被print()函数取代了,可以使用关键字参数来替代老的print特殊语法.例如: Old: print "The answer is", 2*2 New: print("The answer is", 2*2) Old: print x,       

C# 6.0语法新特性体验(二)

之前我在文章通过Roslyn体验C# 6.0的新语法中介绍了一些C# 6.0的语法特性,现在随着Visual Studio 14 CTP3的发布,又陆续可以体验一些新的特性了,这里简单的介绍一下之前没有介绍的新语法. 属性表达式(Property Expressions) 我们常常会在类中写一些通过函数生成的只读属性: ????class Point????{????????public int X { get; set; }????????public int Y { get; set; }

Android Lollipop 新特性 - Palette;获取图片颜色

Android Lollipop 新特性 - Palette 在Android 5.0 之后推出的palette,通过这个方法,我们就可以从一张 bitmap 中提取我们需要的颜色,可以使UI风格更加美观融洽.比如,我们可以从图片中提取颜色设置给ActionBar做背景颜色,这样ActionBar的颜色就会随着显示图片的变化而变化. Palette可以提取的颜色如下: Vibrant (有活力的) Vibrant dark(有活力的 暗色) Vibrant light(有活力的 亮色) Mute

Android5.0 Lollipop正式发布!

今天早些时候,谷歌正式发布了Android 5.0,一同亮相的还有Nexus 6等新品. 跟之前传闻的一样,Android 5.0之所以叫L,其代号是 Lollipop棒棒糖,除了32位版本外,还有64位. 至于Android 5.0的新特性,之前谷歌I/O大会上都已经说的很清楚,比如ART作为默认选项,提高设备续航,新的多任务以及全新的界面设计(material design),当然这次还加入了新的64位版本. 对于5.0来说,谷歌最大的愿景是,统一Android平台.除了Nexus 6和Ne

jdk7和8的一些新特性介绍

jdk7和8的一些新特性介绍 Java代码   本文是我学习了解了jdk7和jdk8的一些新特性的一些资料,有兴趣的大家可以浏览下下面的内容. 官方文档:http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-418459.html 在jdk7的新特性方面主要有下面几方面的增强: 1.jdk7语法上 1.1二进制变量的表示,支持将整数类型用二进制来表示,用0b开头. // 所有整数 int, short,long,byte都可以用二

php7函数,声明,返回值等新特性介绍

使用 ... 运算符定义变长参数函数 (PHP 5 >= 5.6.0, PHP 7) 现在可以不依赖 func_get_args(), 使用 ... 运算符 来实现 变长参数函数. function f($req, $opt = null, ...$params) { // $params 是一个包含了剩余参数的数组 printf('$req: %d; $opt: %d; number of params: %d'."\n", $req, $opt, count($params)