您的位置:首页 > 移动开发 > Android开发

Android Material Design CoordinatorLayout使用

2017-10-24 16:46 435 查看

Android Material Design CoordinatorLayout使用

说到CoordinatorLayout (协调布局)官网解说 他是一个加强版的FrameLayout ,他的应用十分广泛 其主要作用是能够协调子View的相互交互,监听子View的各种事件

CoordinatorLayout 的使用其实很简单就把它当成特殊的FrameLayout 来使用就可以

<?xml version="1.0" encoding="utf-8"?>
<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:fitsSystemWindows="true"
tools:context="com.yuezhi.ap50.MainActivity">

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />

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


java代码区

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_bar_main);
FloatingActionButton fab = (FloatingActionButton)    findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

}


-运行结果:



当单机FloatingActionButton 这个事件会突然发现 这个FloatingActionButton 会随着Snackbar 的移动而移动 这就是这个协调者控件的作用 试想一下 更换一下布局如下CoordinatorLayout 替换FrameLayout 是否还有这个效果

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:fitsSystemWindows="true"
tools:context="com.yuezhi.ap50.MainActivity">

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />

</FrameLayout>


运行结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: