Android CollapsingToolbarLayout字幕

时间:2015-06-16 13:25:16

标签: android subtitle android-design-library androiddesignsupport android-collapsingtoolbarlayout

是否可以像使用普通工具栏一样将字幕设置为CollapsingToolbarLayout?据我所知,没有办法以编程方式执行此操作。另外,如何将白色后箭头设置为工具栏?使用

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

不会显示任何内容,也会添加

app:navigationIcon="@drawable/abc_ic_ab_back_mtrl_am_alpha"

工具栏的

也不会显示它:

2 个答案:

答案 0 :(得分:1)

对于字幕问题,我遇到了类似的问题并发布了我用来修复它的代码:

https://stackoverflow.com/a/31529101/834692

希望你也可能觉得这很有用。

对于后退图标,您需要先设置工具栏,然后调用setDisplayHomeAsUpEnabled():

final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow_back_white);

答案 1 :(得分:0)

有一些方法可以在CollapsingToolbarLayout中添加字幕。

  1. TextView内加CollapsingToolbarLayout,但在Toolbar折叠时,只有标题是CollapsingToolbarLayout的标题,不需要第三方库。
  2. 使用我的图书馆:collapsingtoolbarlayout-subtitle
  3. CollapsingToolbarLayout

    像在任何CollapsingToolbarLayout上一样使用它,并为其添加字幕属性:

    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <com.hendraanggrian.widget.SubtitleCollapsingToolbarLayout
                android:id="@+id/subtitlecollapsingtoolbarlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:contentScrim="?colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:subtitle="CollapsingToolbarLayout"
                app:title="Subtitle">
    
                <!-- collapsing toolbar content goes here -->
    
                <android.support.v7.widget.Toolbar
                    android:layout_width="match_parent"
                    android:layout_height="?actionBarSize"
                    app:layout_collapseMode="pin"/>
            </com.hendraanggrian.widget.SubtitleCollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>
    
        <!-- content goes here -->
    
    </android.support.design.widget.CoordinatorLayout>
    

    哦,关于Toolbar上的后退箭头,我通常在xml中使用?homeAsUpIndicator进行设置:

    <android.support.v7.widget.Toolbar
        ...
        app:navigationIcon="?homeAsUpIndicator"/>
    

    希望它有所帮助。