Appcompat工具栏“android:title”属性不起作用

我确信这是非常简单的,我只是在俯视.如果我在代码中设置标题,它似乎一切正常:
import android.support.v7.widget.Toolbar;
...
// Works fine
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("My Title");

在布局xml中设置它不起作用:

<!-- Doesn't work -->
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:title="@string/my_title"/>

值得注意的是,我正在使用AppCompat v7库并对Android sdk版本18进行测试.

解决方法

您没有使用正确的属性.我认为 the Action Bar docs解释这个最好.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          yourapp:showAsAction="ifRoom"  />
    ...
</menu>

Using XML attributes from the support library

Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library,because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.

换句话说,您需要将android:title更改为yourCustomPrefix:title

<YourRootViewGroup xmlns:app="http://schemas.android.com/apk/res/org.seeingpixels.photon"
    xmlns:android="http://schemas.android.com/apk/res/android"
    ... >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:title="@string/my_title" />

</YourRootViewGroup>

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...