android - 在 android 应用中 Toolbar.setTitle 方法无效 - 应用名称显示为标题

标签 android toolbar android-support-library

我正在尝试使用 android-support-v7:21 库创建简单的应用程序。

代码 fragment :
MainActivity.java

public class MainActivity extends ActionBarActivity {

    Toolbar mActionBarToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);            
        mActionBarToolbar.setTitle("My title");
        setSupportActionBar(mActionBarToolbar);
}

activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar            
        android:id="@+id/toolbar_actionbar"
        android:background="@null"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:fitsSystemWindows="true" />

</LinearLayout>

但工具栏上显示的不是“我的标题”%应用程序名称%。
好像 setTitle 方法没有效果。
我想显示“我的标题”。

统一更新: 之前,styles.xml 是:

<style name="AppTheme" parent="Theme.AppCompat">
    <item name="windowActionBar">false</item>
</style>

所以,我认为没有使用操作栏。 我将 NoActionBar 添加到样式父级:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="windowActionBar">false</item>
</style>

但是问题没有解决。

最佳答案

找到解决办法:

代替:

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);            
mActionBarToolbar.setTitle("My title");
setSupportActionBar(mActionBarToolbar);

我用过:

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);            
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("My title");

而且它有效。

关于android - 在 android 应用中 Toolbar.setTitle 方法无效 - 应用名称显示为标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26486730/

相关文章:

android - Glide centerCrop() 不适用于 CustomTarget

android - 触摸对象时如何在 Canvas 内移动对象?

javascript - MUI - 如何响应地获取 theme.mixins.toolbar.minHeight 值?

android - 带有文本而不是图像的 FloatingActionButton

android - RequiresPermission 注释 'only specify one of value, anyOf or allOf'

android - 绕过 android 5.1 的 android usb 主机权限确认对话框

java - 通知管理器 : cancel(id) DON'T work

android - Autocompletetextview 下拉列表包含 Android L 的 Artifact

java - Swing 工具栏放置

Android 矢量可绘制应用程序 :srcCompat not showing images