微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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>

原文地址:https://www.jb51.cc/android/309079.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐