更改操作栏的标题

时间:2017-12-15 20:18:27

标签: android android-actionbar title

我试图在从其他活动返回时更改操作栏的标题。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    setSupportActionBar(toolbar);
    Log.d("TOOLBAR", getSupportActionBar().getTitle().toString());
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle("title 2");
    Log.d("TOOLBAR 2", getSupportActionBar().getTitle().toString());
    }

在onCreate方法中的位置:

toolbar = findViewById(R.id.toolbar);

例如,如果我之前的标题是"标题1",我可以阅读这些日志:

TOOLBAR: title
TOOLBAR 2: title 2

但是在模拟器中标题没有改变。 我还尝试更改工具栏的标题。

编辑
这是我的活动:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="cct.sss.ViewRecordActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_view_record" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/view_record_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:onClick="editRecord"
        android:tint="@color/white"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@drawable/ic_edit_black_24dp" />

</android.support.design.widget.CoordinatorLayout>

5 个答案:

答案 0 :(得分:1)

如果您想在返回活动时更改Toolbar标题,可以在onResume()中进行更改:

@Override
    protected void onResume() {
        super.onResume();
        getSupportActionBar().setTitle("title 2");
        Log.d("TOOLBAR 2", getSupportActionBar().getTitle().toString());
    }

答案 1 :(得分:0)

替换

getSupportActionBar().setTitle("title 2");

使用

setTitle("title 2");

答案 2 :(得分:0)

尝试删除

setSupportActionBar(toolbar);

onActivityResult方法。

没有必要在这里。

答案 3 :(得分:0)

您不应在setSupportActionBar(toolbar)函数中调用onActivityResult。但是你应该在onCreate中调用它。

要更改操作栏的标题,您只需在toolbar.setTitle("Title 2")内拨打onActivityResult,因为您已在活动中引用toolbar

更新代码如下所示:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    toolbar.setTitle("Title 2");
}

答案 4 :(得分:0)

谢谢大家的回答。我解决了我的问题,只需在onActivityResult()中设置折叠工具栏布局的标题:

collapsingToolbarLayout.setTitle("title");
相关问题