Android SupportActionBar 不刷新标题

标签 android android-actionbar material-design

我在刷新 ActionBar 标题时遇到问题。该应用程序相当简单,目前它只有一项 Activity : `

    <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">

            <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_count_scrolling"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabReset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_alert"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"/>

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

布局 content_count_scrolling 仅包含 NestedScrollViewRecyclerView

我的情况是,当我在 RecyclerView 的任何行的 EditText 中输入一个数字时,它会在数据模型中设置一个值并重新计算总值(所有行的总和)。此总值应设置为 ActionBar.title。为此,我还使用了 RxBus。 MainActivity 如下:

public class MainActivity extends AppCompatActivity {
    // ....
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_scrolling);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setTitle("");

        mRxBus = getRxBusSingleton();        
        // ....
    }
}

public void refreshActionBarTitle() {
    String total = Data.getTotal();
    Timber.d("Refresh bar title with value="+total);

    toolbar.setTitle("");
    setSupportActionBar(toolbar);
    toolbar.setTitle("Title "+total);

    // testes also with:
    // getSupportActionBar().setTitle("Title "+total);

    Timber.d("Title after refresh: " + getSupportActionBar().getTitle());
}

`

方法 refreshActionBarTitle() 由 RxBus 触发。一旦方法被触发,ActionBar 的标题也会被设置(通过日志和调试器检查)。问题是,ActionBar 没有失效和重绘。这是在旋转屏幕后才完成的,这是显而易见的。

所以,请帮助我纠正使工具栏无效的问题。我还必须注意,这个事件将在 EditText 的每次更改后触发,而不仅仅是在更改焦点之后,所以我不能使例如无效。整个屏幕。

我还检查了来自 Toolbar (SupportActionBar) title changes to app name on orientation change 的建议,但他们没有帮助我。

最佳答案

已修复。我应该使用 CollapsingToolbarLayout.setTitle() 方法。这个有效

关于Android SupportActionBar 不刷新标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38672418/

相关文章:

带操作栏的 Android 全屏 Activity

Android MaterialDialog 微调器

angularjs - 如何在 Angular-Material Design 中打开 sideNav

android - 使用 AppCompat v21 将色调应用于 PreferenceActivity 小部件

android - ActionBar 主页按钮 - 如何从 API 11 设置

android - 创建选项卡时我的工具栏出错

angularjs - Angular Material Design - 根据屏幕尺寸更改弹性值

android - android中的消息队列

javascript - 使用react-native-ble-plx包发送数据

android - Arcore fragment 在启动时崩溃