android material 布局,Android应用的Material设计的布局兼容性的一些要点总结

Define Alternative Styles  定义替代样式让你的app,使用Material Design的主题运行在支持它的设备上,并在早期版本的设备上可以运行较早的主题:

1. 在res/values/styles.xml 定义一个主题继承较早的主题

2. 在res/values-v21/styles.xml 定义一个相同名字的继承自Material主题 的主题

3. 在manifest中应用定义的主题

注:如果你的app使用了Material 主题,而不提供较早的主题,那么将不能运行在早期版本的设备上

Provide Alternative Layouts  提供替代布局如果你设计的layout不引用任何的5.0中的xml属性,那么可以运行在早期版本的Android设备上。否则,你可提供一个替代布局。

替代布局建立在res/layout-v21/

为了避免重复代码,可以在res/values/  定义你的styles,新风格的在res/values-21/ 中定义,并使用style的继承,在res/values中定义一个baseStyle,在res/values-21中继承它。

Use the Support Library  使用支持库

v7 support library 包括以下的一些特性:

在应用了一个Theme.AppCompat 主题后,系统的一些组件就有了Material Design 的风格

在Theme.AppCompat 主题中,有调色主题

RecyclerView 组件显示数据集

CardView 组件创建卡片

从图像中取色

System widgets  系统组件

Theme.AppCompat 主题提供的Material Design 风格的组件有:

EditText

Spinner

CheckBox

Radiobutton

SwitchCompat

CheckedTextView

Color Palette

使用v7支持库,获得Material Design 风格定义颜色板,应用一个Theme.AppCompat 主题:

@color/material_blue_500

@color/material_blue_700

@color/material_green_A200

Lists and Cards

使用v7支持库后,在早期的Android版本上也可运行。

Dependencies

gradle 依赖:

dependencies {

compile 'com.android.support:appcompat-v7:21.0.+'

compile 'com.android.support:cardview-v7:21.0.+'

compile 'com.android.support:recyclerview-v7:21.0.+'

}

Check the System Version  检查系统版本

以下特性只能在Android 5.0(API级别21)及以上:

Activity transitions  活动转换

Touch feedback    触觉反馈

Reveal animations  显示动画

Path-based animations  基于路径动画

Vector drawables  矢量图片

Drawable tinting  图片染色

检查代码:

// Check if we're running on Android 5.0 or higher

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

// Call some material design APIs here

} else {

// Implement this feature without material design

}

注:要让app支持5.0,需要在manifest中Android:targetSdkVersion=21。

PS:RecyclerView附RecyclerView的例子:

import android.app.Activity;

import android.os.Bundle;

import android.support.v7.widget.GridLayoutManager;

import android.support.v7.widget.RecyclerView;

import android.support.v7.widget.RecyclerView.LayoutParams;

import android.view.LayoutInflater;

import android.view.ViewGroup;

import android.widget.TextView;

public class RecyclerViewActivity extends Activity {

/*

* recyclerview提供这些内置的布局管理器:

* linearlayoutmanager 显示垂直滚动列表或水平的项目。

* gridlayoutmanager 显示在一个网格项目。

* staggeredgridlayoutmanager 显示在交错网格项目。

* 自定义的布局管理器,需要继承recyclerview.layoutmanager类。

*

* add/remove items时的动画是默认启用的。

* 自定义这些动画需要继承RecyclerView.ItemAnimator,并实现RecyclerView.setItemAnimator()

*/

private RecyclerView mRecyclerView;

private RecyclerView.Adapter mAdapter;

private RecyclerView.LayoutManager mLayoutManager;

private String[] myDataset;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.recycler_view);

mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

// use this setting to improve performance if you know that changes

// in content do not change the layout size of the RecyclerView

mRecyclerView.setHasFixedSize(true);

// use a linear layout manager

// mLayoutManager = new LinearLayoutManager(this);

// mLayoutManager = new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, true);

//true 表示,将layout内容反转

mLayoutManager = new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);

//HORIZONTAL 横向滚动显示内容 VERTICAL纵向

// mLayoutManager = new GridLayoutManager(this, 3, GridLayoutManager.HORIZONTAL, false);

//方向也是指示滚动方向,例子中横向开头的数据交错了一点, 纵向的无交错

// mLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.HORIZONTAL);

// mLayoutManager = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);

mRecyclerView.setLayoutManager(mLayoutManager);

// mRecyclerView.setLayoutManager(new MyLayoutMnager()); //数据不显示,可能还需要重写什么东西。。

// specify an adapter (see also next example)

setDatas();

mAdapter = new MyAdapter(myDataset);

mRecyclerView.setAdapter(mAdapter);

}

private void setDatas() {

int len = 200;

myDataset = new String[len];

for (int i = 0; i < len; i++) {

switch (i%3) {

case 0:

myDataset[i] = "中国" + i;

break;

case 1:

myDataset[i] = "美国" + i;

break;

case 2:

myDataset[i] = "澳大利亚" + i;

break;

}

}

}

class MyLayoutMnager extends RecyclerView.LayoutManager {

@Override

public LayoutParams generateDefaultLayoutParams() {

LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

params.topMargin = 5;

return params;

}

}

class MyAdapter extends RecyclerView.Adapter {

private String[] mDataset;

// Provide a reference to the views for each data item

// Complex data items may need more than one view per item, and

// you provide access to all the views for a data item in a view holder

// Provide a suitable constructor (depends on the kind of dataset)

public MyAdapter(String[] myDataset) {

mDataset = myDataset;

}

// Create new views (invoked by the layout manager)

@Override

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

// create a new view

TextView tv = (TextView) LayoutInflater.from(parent.getContext())

.inflate(R.layout.my_text_view, parent, false);

// set the view's size, margins, paddings and layout parameters

//...

ViewHolder vh = new ViewHolder(tv); //构建一个ViewHolder

return vh;

}

// Replace the contents of a view (invoked by the layout manager)

@Override

public void onBindViewHolder(ViewHolder holder, int position) {

// - get element from your dataset at this position

// - replace the contents of the view with that element

holder.mTextView.setText(mDataset[position]);

}

// Return the size of your dataset (invoked by the layout manager)

@Override

public int getItemCount() {

return mDataset.length;

}

}

static class ViewHolder extends RecyclerView.ViewHolder {

// each data item is just a string in this case

public TextView mTextView;

public ViewHolder(TextView v) {

super(v);

mTextView = v;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值