Help us understand the problem. What is going on with this article?

CollapsingToolbarLayout のタイトルの配置を指定する

More than 3 years have passed since last update.

AndroidStudio のActivityテンプレート ScrollingActivity などでも使われている
CollapsingToolbarLayout を使用したレイアウトを作成した場合、
特に指定のない場合は下記の画像のようにタイトルが少し浮いた位置に配置されます。

collapsing_toolbar_02.png

CollapsingToolbarLayout 内のタイトルの配置を指定したい場合、
app:expandedTitleGravity で位置を指定することができます。
また app:expandedTitleGravity で指定しただけではまだタイトル下部にスペースが空いているので
app:expandedTitleMarginBottom で CollapsingToolbarLayout の下部からのスペースを指定します。
(app:expandedTitleMarginBottom="0dp" で下部にぴたっとくっつきます。)

例えば

<android.support.design.widget.CollapsingToolbarLayout
    app:expandedTitleGravity="bottom|center_horizontal"
    app:expandedTitleMarginBottom="8dp" />

を指定すると下記の画像のような配置にタイトルを置くことができます。

collapsing_toolbar_01.png

レイアウト全体のコードです。

<android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.AppBarLayout>
        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:expandedTitleGravity="bottom|center_horizontal"
            app:expandedTitleMarginBottom="8dp"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:expandedTitleTextAppearance="@style/MyTextAppearanceCollapsingToolbar">
        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin" />
        <android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <!-- contents -->

</android.support.design.widget.CoordinatorLayout>
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away