安卓Design包之AppBar和Toolbar的联用
前面讲了Design包的的CoordinatorLayout和SnackBar的混用,现在继续理解Design包的AppBar;
AppBarLayout跟它的名字一样,把容器类的组件全部作为AppBar.
如:
<android.support.design.widget.AppBarLayout android:layout_width="match_parent" app:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar app:layout_scrollFlags="scroll|enterAlways" android:layout_width="match_parent" android:layout_height="?actionBarSize" android:id="@+id/main_toolbar"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="悬停条目"/> </android.support.design.widget.AppBarLayout>
这里就是把Toolbar和TextView放到了AppBarLayout中,让他们当做一个整体作为AppBar。
引用appBar的
app:layout_scrollFlags="scroll|enterAlways"
属性,则可实现下拉的时候不显示appbar,上拉的时候显示appbar。这个在用户交互上真的挺好用的。
再来看看可折叠的ToolBar
<android.support.design.widget.AppBarLayout android:layout_width="match_parent" app:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar" android:layout_height="wrap_content"> <!--可折叠的toolbar--> <android.support.design.widget.CollapsingToolbarLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:contentScrim="@color/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView android:layout_width="match_parent" android:layout_height="200dp" app:layout_collapseMode="parallax" android:src="@mipmap/ic_launcher"/> <android.support.v7.widget.Toolbar app:layout_scrollFlags="scroll|enterAlways" android:layout_width="match_parent" android:layout_height="?actionBarSize" android:id="@+id/main_toolbar"/> </android.support.design.widget.CollapsingToolbarLayout> <!--<TextView--> <!--android:layout_width="match_parent"--> <!--android:layout_height="wrap_content"--> <!--android:text="悬停条目"/>--> </android.support.design.widget.AppBarLayout>
非常的简单。只需要在上面的代码中修改一下就可以了。
作 者:
南 尘
出 处: http://www.cnblogs.com/liushilin/
关于作者:专注于移动前端的项目开发。如有问题或建议,请多多赐教!欢迎加入Android交流群:118116509
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信我
声援博主:如果您觉得文章对您有帮助,可以点击文章下部【推荐】或侧边【关注】。您的鼓励是作者坚持原创和持续写作的最大动力!
欢迎关注我的公众号,精讲面试、算法、Andrid、Java、Python,旨在打造全网最比心的公众号。