无法设置位于AppBarLayout内部的工具栏标题

10

我在 AppBarLayout 中有一个工具栏,如下所示:

<android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="right"
                android:orientation="horizontal">

                <ImageButton
                    android:id="@+id/editButton"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_gravity="center_vertical"
                    android:layout_marginRight="16dp"
                    android:scaleType="fitCenter"
                    android:background=
                        "?attr/selectableItemBackgroundBorderless"
                    android:contentDescription="Post"
                    android:src="@drawable/ic_mode_edit_24dp"
                    />

            </LinearLayout>

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

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

在我的Activity.OnCreate()中,我尝试像这样设置标题:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Edit");

不确定是否重要,但之后我会膨胀一个片段:

MyFragment fragment = new MyFragment();
fragment.setArguments(savedInstanceState);

// Add fragment with tag
getSupportFragmentManager().beginTransaction()
    .add(R.id.content_frame, fragment, "my_frag")
    .commit();

然而,这没有任何效果。事实上,根本就没有标题。这是因为我在Toolbar内部有一个LinearLayout吗?还是因为AppBarLayout的原因?


1
你为什么需要将LinearLayout作为Toolbar的子项,而不是使用菜单呢? - tachyonflux
我也想知道同样的事情,我一直在尝试使用应用程序活动中的标签来识别其目的以保持其清晰。当活动内的片段大幅改变活动的目的时,我可以想象这会非常有用。 - Priyeshj
4个回答

13

你需要在 ToolBar 内添加一个 TextView

编辑:类似这样

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:contentInsetEnd="0dp"
        android:contentInsetLeft="0dp"
        android:contentInsetRight="0dp"
        android:contentInsetStart="0dp"
        android:elevation="2dp"
        app:contentInsetEnd="0dp"
        app:contentInsetLeft="0dp"
        app:contentInsetRight="0dp"
        app:contentInsetStart="0dp"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/beermap"
            android:textColor="@color/white"
            android:textSize="22sp"
            android:textStyle="bold" />

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

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

TextView 有什么特别之处需要添加吗?我的意思是我希望它看起来像活动的默认样式标题。 - Micro
不,它只是一个普通的TextView。然后你可以根据自己的喜好进行样式设置,使其看起来像一个“常规”标题。 - Matias Elorriaga
我会接受这个答案,因为它解决了我的问题,但是一个新的问题是如何将那个 TextView 设计成与“常规”的那个一样! - Micro
1
还是有点不对劲。我试了20sp,那样更接近了,但是无法完全调整好字体。哦,算了,谢谢。 - Micro

4

在活动中使用getSupportActionBar().setTitle(getString(R.string.title));。 这可以在onCreate中完成,应该在更改语言环境时执行。


3
如果您的工具栏位于可折叠的工具栏布局内部。
    public void setToolbarTitle(String s){
    Log.d(TAG, "setToolbarTitle: " + s);

    CollapsingToolbarLayout collapsingToolbarLayout = findViewById(R.id.collapsingToolbar);
    collapsingToolbarLayout.setTitle(s);
}

2

以下是如何使TextView看起来与默认样式完全相同的方法:

<TextView
            android:id="@+id/toolbar_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/beermap"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:fontFamily="sans-serif-medium" />

关键部分是20sp字号和android:fontFamily="sans-serif-medium"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接