Material Design 设计规范在 Google I/O 2014 推出,这种设计理念一经推出就受到广大开发者的喜爱,主要侧重于纸墨化创作和突出设计的实体感,使得设计更接近于真实世界,力求平滑
CoordinatorLayout
作为应用的顶层布局
作为与一个或多个子 View 交互的容器
fab.setImageDrawable(getResources().getDrawable(R.drawable.fab));
fab.setImageResource(R.drawable.fab);
fab.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#000000" )));
fab.setSize(FloatingActionButton.SIZE_MINI);
private int getSizeDimension (@Size final int size) {
final Resources res = getResources();
switch (size) {
case SIZE_AUTO:
final int width = res.getConfiguration().screenWidthDp;
final int height = res.getConfiguration().screenHeightDp;
return Math.max(width,height) < AUTO_MINI_LARGEST_SCREEN_WIDTH
? getSizeDimension(SIZE_MINI)
: getSizeDimension(SIZE_NORMAL);
case SIZE_MINI:
return res.getDimensionPixelSize(R.dimen.design_fab_size_mini);
case SIZE_NORMAL:
default :
return res.getDimensionPixelSize(R.dimen.design_fab_size_normal);
}
}
/**dimens.xml**/
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools ="http://schemas.android.com/tools" >
<dimen name ="design_fab_size_mini" tools:override ="true" > 20dp</dimen >
<dimen name ="design_fab_size_normal" tools:override ="true" > 100dp</dimen >
</resources >
android:src //设置图标(24dp)
app:backgroundTint //设置图标背景颜色。
app:rippleColor //设置点击时水波纹颜色
app:elevation //设置阴影大小
app:fabSize //设置大小
app:pressedTranslationZ //按下时距离Z轴的距离
app:layout_anchor //设置锚点
app:layout_anchorGravity//设置相对锚点的位置
简单使用
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android ="http://schemas.android.com/apk/res/android"
xmlns:tools ="http://schemas.android.com/tools"
android:layout_width ="match_parent"
android:layout_height ="match_parent"
xmlns:app ="http://schemas.android.com/apk/res-auto"
tools:context ="com.manu.materialdesignsamples.samples.SampleActivity" >
<android.support.v7.widget.RecyclerView
android:id ="@+id/rvData"
android:layout_width ="match_parent"
android:layout_height ="wrap_content"
android:layout_marginStart ="12dp"
android:layout_marginEnd ="12dp"
android:visibility ="visible" />
<android.support.design.widget.FloatingActionButton
android:id ="@+id/fab"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_margin ="12dp"
android:layout_gravity ="bottom|end"
android:src ="@drawable/fab"
android:scaleType ="center"
app:backgroundTint ="@color/colorAccent"
app:backgroundTintMode ="src_in"
app:elevation ="5dp"
app:rippleColor ="#000000"
app:fabSize ="auto"
app:pressedTranslationZ ="10dp" />
</android.support.design.widget.CoordinatorLayout >
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
Snackbar.make(v,"我是Snackbar..." ,Snackbar.LENGTH_SHORT)
.setAction("取消" ,new View.OnClickListener() {
@Override
public void onClick (View v) {
Toast.makeText(SampleActivity.this ,"取消" ,Toast.LENGTH_SHORT).show();
}
}).show();
}
});