在可收合的 App Bar 中加入 Subtitle

可收合的 App Bar (以前叫 Action Bar 后来又一度改成 Tool Bar) 是 Android 平台上新推出的 Material Design 效果。要在 App 中使用这个效果并不难,只要在最新的 Android Studio 中,于新增 Activity 时选择【File -> New -> Activity -> Scrolling Activity】,并依照 “Configure Activity” 窗口的栏位填好内容,按下【Finish】按钮,就可以有一个运行起来如下图的画面,颇为无脑。

只不过如果要在展开的 App Bar 上加入一个副标题,让画面看起来丰富一点,就有一点烧脑了!调用 setSubtitle 函式在这样的画面配置下并不管用,所以必须要在 Layout 的内容上做一些改变。

首先,要先让 App Bar 展开后的 Title 往上提一点,以便挪出空间可以容纳副标题的文字,这个效果可以用 expandedTitleMarginBottom 的属性来达成, Layout 内容如下:

<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/toolbar_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    app:contentScrim="?attr/colorPrimary"
    app:expandedTitleMarginBottom="40dp"
    app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_collapseMode="pin"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

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

运行后,Activity 的画面就像下图一样:

接下来就是要把副标题加到标题下方,这里使用 TextView 来达成。要注意的是,TextView 的 Layout 内容必须要加在 CollapsingToolbarLayout 之内,并且 layout_gravity 的属性要设为 Bottom。为了要对齐主标题的缩进,paddingStart 属性的内容要设成 32dp。Textview 的内容如下:

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:paddingStart="32dp"
        android:paddingEnd="8dp"
        android:paddingBottom="8dp"
        android:text="Subtitle goes here"
        android:textColor="@android:color/white"
        android:textSize="20sp"/>

加好了之后,运行,Activity 就会出现如下图的画面:

不过,这里有一点不太完美的地方,在收合到最上方的动画会出现 Subtitle 的文字与收合后的 Title 重叠。虽然无伤大雅,但还是让人觉得介意。还好要解决并不困难,Android 的 SDK 里就已经有现成的方式来处理,就是将 layout_collapseMode 属性套用在 TextView 上,把内容设定为 parallax 就搞定了。以下是完整的 Layout 内容:

<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/toolbar_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    app:contentScrim="?attr/colorPrimary"
    app:expandedTitleMarginBottom="40dp"
    app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:paddingStart="32dp"
        android:paddingEnd="8dp"
        android:paddingBottom="8dp"
        android:text="Subtitle goes here"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        app:layout_collapseMode="parallax"/>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_collapseMode="pin"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.CollapsingToolbarLayout>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,615评论 25 709
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,752评论 0 17
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,269评论 19 139
  • 今天误打误撞进了北师大附属实验中学 小盆友们正在上体育课 觉得好亲切 讲真 以前为什么一点也不喜欢上体育课 现在看...
    梁绛阅读 334评论 2 0
  • 今天,想提一个女孩-西 Liu Yun 一个三观很正的女孩子,善良,温婉,单纯却不会纵容
    多多森阅读 360评论 0 0