【问题标题】:Android toolbar show title and subtitle only when AppBarLayout collepsedAndroid工具栏仅在AppBarLayout折叠时显示标题和副标题
【发布时间】:2016-02-06 13:40:48
【问题描述】:

我有 AppBarLayout 、CollapsingToolbarLayout 和工具栏的活动。 从代码中设置标题和副标题。最初我希望隐藏工具栏并在 Appbar 布局折叠时显示, 使用我的代码它的工作(工具栏最初隐藏)但它总是显示工具栏标题和副标题。仅当 appbar 布局完全折叠时如何显示标题

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

【问题讨论】:

  • 你必须在CollapsingToolbarLayoutsetTitle。不要直接访问ToolbarActionBar。注意CollapsingToolbarLayout 不支持字幕。
  • 我怎样才能实现这个功能
  • 选项 a) 在此处提交问题code.google.com/p/android/issues/list,选项 b) 开源 CollapsingToolbarLayout 的源代码,了解它的工作原理并添加您需要的功能。如果您选择这种方式,请考虑将您的解决方案开源,因为其他人可能有类似的需求。谢谢!
  • 选项 c) 不要使用字幕。使用两行标题,第二行用AbsoluteSizeSpanRelativeSizeSpan 包裹。
  • 感谢 Eugen 的指导

标签: android android-toolbar android-collapsingtoolbarlayout android-appbarlayout


【解决方案1】:

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

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 编写的,因此可能需要稍作修改)

【讨论】:

    【解决方案2】:

    我在 xml 中使用 ControllableAppBarLayout 解决了我的问题,并使用以下方法处理它的 EXPAND、COLLAPSED、IDEAL 事件以显示/设置我的 TITLE 和 SUBTITLE。

     ControllableAppBarLayout appBarLayout = (ControllableAppBarLayout) findViewById(R.id.app_bar);
        appBarLayout.setOnStateChangeListener(new ControllableAppBarLayout.OnStateChangeListener() {
    
            @Override
            public void onStateChange(ControllableAppBarLayout.State toolbarChange) {
                switch (toolbarChange) {
    
                    case COLLAPSED: {
                        Log.i(TAG, "COLLAPSED2");
                        if (mProfileDetails != null) {
                            getSupportActionBar().setTitle(mProfileDetails.userDetails.userFullname);
                            getSupportActionBar().setSubtitle(Html.fromHtml("<small>" + mProfileDetails.userDetails.headline + "</small>"));
                        }
                        break;
                    }
                    case EXPANDED:
                        Log.i(TAG, "EXPANDED");
                        getSupportActionBar().setTitle("");
                        getSupportActionBar().setSubtitle("");
                        break;
    
                    case IDLE: // Just fired once between switching states
                        Log.i(TAG, "IDLE");
                        getSupportActionBar().setTitle("");
                        getSupportActionBar().setSubtitle("");
                        break;
                }
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-30
      • 2015-10-18
      • 2015-12-28
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多