在android应用程序中 Toolbar.setTitle 方法无效 - 应用程序名称显示为标题 [英] In android app Toolbar.setTitle method has no effect – application name is shown as title

查看:32
本文介绍了在android应用程序中 Toolbar.setTitle 方法无效 - 应用程序名称显示为标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

代码片段:
主Activity.java

I'm trying to create simple application using android-support-v7:21 library.

Code snippets:
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

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 方法没有效果.
我想显示我的标题".

But instead of "My title" on Toolbar %application name% is shown.
Seems like setTitle method has no effect.
I would like to show "My title".

UPD:之前,styles.xml 是:

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

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

So, I thought that actionbar is not used. I add NoActionBar to style parent:

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

但是问题没有解决.

推荐答案

找到解决方案:

Found the solution:

代替:

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应用程序中 Toolbar.setTitle 方法无效 - 应用程序名称显示为标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆