Android 如何在折叠工具栏中将文本视图转换为标题?
我有一个Android 如何在折叠工具栏中将文本视图转换为标题?,android,android-toolbar,android-collapsingtoolbarlayout,Android,Android Toolbar,Android Collapsingtoolbarlayout,我有一个折叠工具栏布局,它在文本视图下包含一个文本视图和一个分级栏。是否可以将TextView转换为标题 我当前的布局如下所示: 我希望“标题”在工具栏折叠时向上转换。还有一个后退按钮,那么如何确保textview转换到正确的位置(后退按钮的右侧) 编辑:这是我的XML <android.support.design.widget.CoordinatorLayout android:id="@+id/main_content" xmlns:android="http://schemas.a
折叠工具栏布局
,它在文本视图
下包含一个文本视图
和一个分级栏
。是否可以将TextView
转换为标题
我当前的布局如下所示:
我希望“标题”在工具栏折叠时向上转换。还有一个后退按钮,那么如何确保textview转换到正确的位置(后退按钮的右侧)
编辑:这是我的XML
<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="@dimen/detail_view_app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleTextAppearance="@style/CollapsingToolbarLayoutExpandedTextStyle"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginLeft="@dimen/double_margin"
android:gravity="center_vertical"
android:orientation="vertical"
app:layout_collapseMode="parallax">
<TextView
android:id="@+id/title"
fontPath="fonts/OpenSans-Bold.ttf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textColor="@android:color/white"
android:textSize="@dimen/large_text"/>
<RatingBar
android:id="@+id/rating_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
解决方案是:不要使用
TextView
通过使用以下属性,可以将展开的标题放置在任何位置:
app:expandedTitleGravity default is bottom|left -or- bottom|start
app:expandedTitleMargin
app:expandedTitleMarginBottom
app:expandedTitleMarginStart
app:expandedTitleMarginEnd
我假设您使用的是NoActionBar
主题,这就是为什么您开始使用TextView
作为标题的原因
这是您的布局,已修复为使用折叠工具栏布局
标题:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="@dimen/detail_view_app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleTextAppearance="@style/CollapsingToolbarLayoutExpandedTextStyle"
app:expandedTitleMarginBottom="64dp"
app:expandedTitleMarginStart="16dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<!--
set the expandedTitleMarginBottom to a value
that positions the expanded title above the rating bar
-->
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<RatingBar
android:id="@+id/rating_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginLeft="16dp"
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/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
您可以发布布局XML吗?您可能不需要为标题使用单独的文本视图就可以得到所需的内容。@krislarson我添加了xml。我一直在研究您的解决方案,但还没有可用的示例。其基本思想是,标题不需要文本视图;您可以添加一个支持工具栏,然后在折叠工具栏布局上设置标题。当工具栏折叠时,标题将转换到正确的位置。您可以使用
android:expandedTitleMargin
参数设置展开标题的位置,因此它位于分级栏上方。查看Chris Banes的cheesesquare演示中的详细活动,了解这一点:我如何定位它,使评级栏位于标题下方?您可以在折叠工具栏布局上使用app:expandedTitleMarginBottom
属性。(这不是我前面评论中的android前缀。)留出足够大的边距,以便标题清除评级栏。您可以将展开的标题定位到几乎任何您想要的位置,当工具栏折叠时,标题会返回到通常的位置。正如您所提到的,您可以使用这些折叠工具栏布局属性将展开的标题定位到任何您想要的位置。通过使用此标题,位置会发生变化,但工具栏仅存在于该位置。因此,我的评级栏单击不起作用,因为它隐藏在工具栏下(但对我们不可见)。如何解决这个问题?@Raj如果不看你的代码,很难判断,但你可能希望你的评级栏位于滚动视图
或其他滚动视图中,该视图位于AppBarLayout
的下方,但位于CoordinatorLayout
的内部。请提交一个包含代码相关部分的新问题,以便社区能够帮助您。你可以在对这个问题的评论中发布一个你的问题的链接,我会看一看。
CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);
collapsingToolbar.setTitle("Title");