使用includes for toolbar.xml时无法设置工具栏标题

时间:2015-06-22 10:41:02

标签: android android-support-library android-toolbar

我正在使用以下设置来主题化toolbar.xml中的工具栏:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimaryDark"
    android:elevation="4dp"
    app:title="@string/app_name"
    >

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

在布局my_layout.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >

    <include
        app:title="@string/my.toolbar.title"
        layout="@layout/toolbar"
        >
    </include>
</LinearLayout>

不幸的是,工具栏的标题没有得到my.toolbar.title的值。

http://developer.android.com/training/improving-layouts/reusing-layouts.html#Include建议您可以覆盖任何android:属性,但app:。

注意:我也尝试使用android:title而不是app:title,但仍然没有成功。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您可以通过编程方式执行此操作:

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

答案 1 :(得分:0)

您需要使用工具栏周围的<merge>标记,这样您就可以使用包含标记中工具栏上使用的标记。