Android工具栏仅在AppBarLayout崩溃时显示标题和副标题 [英] Android toolbar show title and subtitle only when AppBarLayout collepsed

查看:176
本文介绍了Android工具栏仅在AppBarLayout崩溃时显示标题和副标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有AppBarLayout,CollapsingToolbarLayout和工具栏活动. 通过代码设置标题和字幕.最初,我希望工具栏隐藏起来,并在应用栏布局折叠时显示, 使用我的代码,它可以工作(最初隐藏工具栏),但始终显示工具栏标题和副标题.仅在应用栏布局完全折叠时如何显示标题

I have activity with AppBarLayout ,CollapsingToolbarLayout and toolbar. Setting title and subtitle from code. Initially i want toolbar hidden and show when Appbar layout collapsed, With my code its working (toolbar hide initially) but its showing toolbar title and subtitle always. How do i show title only when appbar layout collapse completely

<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:titleEnabled="false"
        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>

设置标题和字幕

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setTitle("Title");
    getSupportActionBar().setSubtitle("sutitle");

推荐答案

一个简单的AppBarLayout.OnOffsetChangedListener应该仅使用内置视图来解决问题:

A simple AppBarLayout.OnOffsetChangedListener should do the trick using only built-in views:

AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener {
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int offset) {
            ActionBar actionBar = getSupportActionBar();
            boolean toolbarCollapsed = Math.abs(offset) >= appBarLayout.getTotalScrollRange();
            actionBar.setTitle(toolbarCollapsed ? yourTitle : "");
            actionBar.setSubtitle(toolbarCollapsed ? yourSubTitle : "");
        }
});

(此代码最初是用C#(Xamarin)而不是Java编写的,因此可能需要进行少量修改)

(This code originally was written in C# (Xamarin), not Java, so minor modifications may be needed)

这篇关于Android工具栏仅在AppBarLayout崩溃时显示标题和副标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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