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>

以上是来客网为你收集整理的Appcompat工具栏“android:title”属性不起作用全部内容,希望文章能够帮你解决Appcompat工具栏“android:title”属性不起作用所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。