【问题标题】:Android SupportActionBar does not refresh titleAndroid SupportActionBar 不刷新标题
【发布时间】:2016-07-30 09:30:05
【问题描述】:

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

    <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 仅包含 NestedScrollView 内部有 RecyclerView

我的情况是,当我在 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 的建议,但他们没有帮助我。

【问题讨论】:

  • 尝试添加getSupportActionBar().setDisplayShowTitleEnabled(true);
  • 我想我也做到了。或者更确切地说,使用false 检查它,然后使用true 强制执行无效。没用。我也试过Toolbar 无效,并设置抽屉。而且什么也没有。

标签: android android-actionbar material-design


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    相关资源
    最近更新 更多