【问题标题】:Using CollapsingToolbarLayout, how can I set it as the support action bar?使用 CollapsingToolbarLayout,如何将其设置为支持操作栏?
【发布时间】:2016-11-25 05:56:29
【问题描述】:

按照this 教程,我有这个CollapsingToolbarLayout

<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:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">

        <ImageView
            android:id="@+id/blurred_background"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:ignore="ContentDescription" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/defaultimage"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.7"
            tools:ignore="ContentDescription"
            android:id="@+id/profilePicture" />


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

但是,我需要使用setSupportActionBar(),这样我才能使用setTitle()setHomeAsUpEnabled()等方法。但是,显然CollapsingToolbarLayout 不能转换为Toolbar,并且setSupportActionBar() 方法只接受Toolbar 方法。

有替代的设置方法吗?

【问题讨论】:

  • 为什么不把你的ImageViews 包裹在Toolbar 中?
  • @MikeM。好主意,但我的ImageViews 似乎失去了他们所拥有的对齐方式。 (其中一个是居中的,现在它在屏幕的左侧`
  • 在 ToolBar 中,您也可以像在 LinearLayout 中一样添加所需的子视图,所以是的,您为什么不使用它?
  • 谢谢@MikeM。欣赏它。

标签: android android-toolbar android-collapsingtoolbarlayout


【解决方案1】:

尝试做这样的事情:

 <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginBottom="70dp"
        app:expandedTitleMarginEnd="10dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_collapseParallaxMultiplier="0.5"
        app:layout_scrollFlags="scroll|enterAlways|snap">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:gravity="bottom"
            android:orientation="vertical"
            app:layout_collapseMode="parallax">

            <ImageView
              android:id="@+id/blurred_background"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              tools:ignore="ContentDescription" />

           <ImageView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:adjustViewBounds="true"
              android:src="@drawable/defaultimage"
              app:layout_collapseMode="parallax"
              app:layout_collapseParallaxMultiplier="0.7"
              tools:ignore="ContentDescription"
              android:id="@+id/profilePicture" />
        </LinearLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:contentInsetStartWithNavigation="0dp"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
    </android.support.design.widget.CollapsingToolbarLayout>

在 LinearLayout/RelativeLayout 中添加您的图像视图,并在 CollapsingToolbarLayout 中添加 Toolbar 以实现 Toolbar 功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 2015-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多