android - 使用toolbar.xml 的include 时无法设置工具栏标题

标签 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 但仍然没有成功。

有什么想法吗?

最佳答案

您可以通过编程来完成:

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

关于android - 使用toolbar.xml 的include 时无法设置工具栏标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30977875/

相关文章:

android - 如何使用 Android 中的折叠工具栏布局使此个人资料图片移动

安卓工作室 : How to find latest version number of google specific libraries?

android - 在工具栏上为 Activity 和 fragment 显示不同的菜单选项

android - 第二个工具栏的 scrollFlag 属性被第一个工具栏应用

android - 再次显示工具栏和底部导航 View

android - “All com.android.support libraries must use the exact same version specification”不想离开

android - 将数据从 Android 传输到 MS SQL Server

Android @RestService 通过 androidannotations 注入(inject)导致 nullPointerException

android - getAdapterPosition() 不返回项目在 RecyclerView 中的位置

android - Android-L 预览版中的 AppCompat/Chromecast