Android Material Design控件之CoordinatorLayout

Android Material Design Android官方控件学习

CoordinatorLayout

  • CoordinatorLayout is a super-powered FrameLayout.
  • CoordinatorLayout是一个增强型的FrameLayout,所以用法跟FrameLayout一样的。
  • 通过自定义子布局的Behaviors(行为)来实现控件之间的交互动画效果,这是不同于FrameLayout的一点,跟其他MD控件(如下面要将的AppBarLayout等)一起使用,实现MD风格交互效果

导入design库

'com.android.support:design:26.1.0'

先介绍跟FrameLayout一样的用法

  • 结合Toolbar和NestedScrollView
  • NestedScrollView需要加上android:layout_marginTop="?actionBarSize",不让会覆盖Toolbar,果然就是FrameLayout的特性
<android.support.design.widget.CoordinatorLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:app="http://schemas.android.com/apk/res-auto"
	xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
>
	<android.support.v7.widget.Toolbar
		android:id="@+id/toolbar"
		android:background="?attr/colorPrimary"
		android:layout_width="match_parent"
		app:theme="@style/ThemeOverlay.AppCompat.Dark"
		android:layout_height="wrap_content">

	</android.support.v7.widget.Toolbar>


	<android.support.v4.widget.NestedScrollView
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:layout_marginTop="?actionBarSize"
	>

		<LinearLayout
			android:layout_width="match_parent"
			android:orientation="vertical"
			android:layout_height="match_parent">

			<TextView
				android:layout_width="match_parent"
				android:text="这里简单展示跟FrameLayout一样的用法,增强behavior用法请看下面AppBarLayoutActivity等"
				android:background="#cdcdcd"
				android:layout_height="400dp"/>
			<TextView
				android:layout_width="match_parent"
				android:background="#ababab"
				android:layout_height="400dp"/>
			<TextView
				android:layout_width="match_parent"
				android:background="#fafafa"
				android:layout_height="400dp"/>
		</LinearLayout>


	</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

不使用Behaviors的CoordinatorLayout跟咸鱼一样

下面将AppBarLayout时,你才会发现CoordinatorLayout的神奇力量.
to be continue…

github 源码地址:https://github.com/LinweiJ/MaterialDesignWidget

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章