簡體   English   中英

使用CollapsingToolbarLayout將視圖添加到工具欄時,會刪除以編程方式設置的標題

[英]Adding views to Toolbar removes the programatically set title when using the CollapsingToolbarLayout

當我將視圖添加到工具欄時,使用setTitle()添加的以編程方式添加的標題會消失。

<android.support.design.widget.CollapsingToolbarLayout
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7"
                app:statusBarScrim="@color/colorAccent"
                app:layout_scrollFlags="scroll"
                android:fitsSystemWindows="true"
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:expandedTitleMarginTop="32dp"
                app:expandedTitleMarginBottom="32dp"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="48dp"


                >


                <android.support.v7.widget.Toolbar
                    android:minHeight="0dp"
                    android:fitsSystemWindows="true"
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

                    >

 <!--IF I DELETE THE VIEWS IN THE TOOLBAR THE PROGRAMATICALLY SET TITLE COMES BACK-->

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">


                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textColor="#fff3f3f3"
                         android:textAppearance="@style/TextAppearance.AppCompat.Title"
                        android:gravity="center"
                        android:textSize="20sp"
                        android:text="Profile"
                        />

                    </LinearLayout>
                </android.support.v7.widget.Toolbar>

我見過其他人也遇到同樣的問題,但找不到解決方案。

如果有人可以幫助我或向我展示一些我可以用來解決問題的材料,我將不勝感激。

謝謝

Toolbar內部有一個TextView ,因此,如果調用setTitle它將文本設置為該TextView

如果您想要一個自定義TextView ,只需為其提供一個ID,找到它,然后為其設置文本。

...
<TextView android:id="@+id/toolbar_title"/>
...

然后,

Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
TextView title = (TextView) toolbarTop.findViewById(R.id.toolbar_title);
title.setText("Title");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM