gpt4 book ai didi

android - CollapsingToolbarLayout.setTitle(title) 在更改 appbarLayout 后不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:52:15 26 4
gpt4 key购买 nike

我想加载一个带有折叠的 collapsingToolbar 的 fragment (即以其未展开的形式)。当我在不更改 appbarLayout 布局参数的情况下设置 collapsingToolbar 的标题时,标题设置正确并且我可以看到标题。

问题是,我还需要更改 appBarLayout 布局参数以防止 collapsingToolbar 扩展(即,我希望它看起来和行为都像这个特定 fragment 的常规非折叠工具栏)。但是,这样做会使标题不再出现。

我尝试过的:我已尝试在这些页面上列出的解决方案无济于事:

MainActivity.java我相信我已经将问题隔离到这些行,但我不确定如何解决它。

collapsingToolbarLayout.setTitle("All Recent"); // works
appBarLayout.setExpanded(false, true); // works

// However, after adding the following lines, the above no longer works and the title does not appear in the toolbar
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appBarLayout.getLayoutParams();
lp.height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics());
appBarLayout.setLayoutParams(lp);

activity_main.xml

<!-- language: lang-xml -->

<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/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/detail_backdrop_height"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">

<ImageView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
android:adjustViewBounds="true" />

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:title="@string/drawer_item_locate_events" />

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

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

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<FrameLayout
android:id="@+id/frame_fragments"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</android.support.v4.widget.NestedScrollView>

<android.support.design.widget.FloatingActionButton
android:id="@+id/floating_action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:clickable="true"
android:src="@mipmap/ic_add_a_photo"
app:layout_anchor="@+id/appbar_layout"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>

更改 appbarLayout 的参数后标题不再显示

Title no longer shows after altering appbarLayout's parameters

最佳答案

我能够使用@DanielPersson 在 how to pin title in Toolbar inside CollapsingToolbarLayout 提供的以下答案解决此问题。 :

collapsingToolbarLayout.setTitleEnabled(false);
toolbar.setTitle("My Title");

通过调用setTitleEnabled(false);,标题出现在工具栏中。

关于android - CollapsingToolbarLayout.setTitle(title) 在更改 appbarLayout 后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37600497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com