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

前端之家收集整理的这篇文章主要介绍了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相关文章