collapsingToolbarLayout仅在折叠时设置标题

时间:2015-08-22 12:20:12

标签: android listener toolbar android-collapsingtoolbarlayout

我只想在崩溃时设置app:expandedTitleTextAppearance="@android:color/transparent" 标题

为此我设置:

STACK_TRACE

java.lang.IllegalArgumentException: width and height must be > 0
    at android.graphics.Bitmap.createBitmap(Bitmap.java:687)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:666)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:633)
    at android.support.design.widget.CollapsingTextHelper.ensureExpandedTexture(CollapsingTextHelper.java:405)
    at android.support.design.widget.CollapsingTextHelper.setInterpolatedTextSize(CollapsingTextHelper.java:382)
    at android.support.design.widget.CollapsingTextHelper.calculateOffsets(CollapsingTextHelper.java:227)
    at android.support.design.widget.CollapsingTextHelper.setExpansionFraction(CollapsingTextHelper.java:203)
    at android.support.design.widget.CollapsingToolbarLayout$OffsetUpdateListener.onOffsetChanged(CollapsingToolbarLayout.java:754)
    at android.support.design.widget.AppBarLayout$Behavior.dispatchOffsetUpdates(AppBarLayout.java:851)
    at android.support.design.widget.AppBarLayout$Behavior.setAppBarTopBottomOffset(AppBarLayout.java:834)
    at android.support.design.widget.AppBarLayout$Behavior.scroll(AppBarLayout.java:793)
    at android.support.design.widget.AppBarLayout$Behavior.onNestedScroll(AppBarLayout.java:644)
    at android.support.design.widget.AppBarLayout$Behavior.onNestedScroll(AppBarLayout.java:583)
    at android.support.design.widget.CoordinatorLayout.onNestedScroll(CoordinatorLayout.java:1428)
    at android.support.v4.view.eh.a(ViewParentCompat.java:97)
    at android.support.v4.view.ec.a(ViewParentCompat.java:330)
    at android.support.v4.view.bk.a(NestedScrollingChildHelper.java:162)
    at android.support.v7.widget.RecyclerView.dispatchNestedScroll(RecyclerView.java:8306)
    at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1387)
    at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2209)

它运作良好。

但是在4.2.2应用程序崩溃时跟随Log

Listener

请告诉我正确的实施方法。

OR

是否有collapsingToolbarLayout来检测ToolBar是否已崩溃,因此我可以隐藏/取消隐藏collapsingToolbarLayout标题...

OR

是否可以将{{1}}标题与我想要的另一个视图对齐...

3 个答案:

答案 0 :(得分:16)

最终CollapsingToolbarLayout如下: -

collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
collapsingToolbarLayout.setTitle("Create Delivery Personnel");
collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(R.color.transperent));
collapsingToolbarLayout.setCollapsedTitleTextColor(Color.rgb(0, 0, 0));

答案 1 :(得分:4)

您可以使用此方法向AppBar添加监听器,您可以倾听或不收听。

final Toolbar tool = (Toolbar)findViewById(R.id.toolbar);
CollapsingToolbarLayout c = (CollapsingToolbarLayout)findViewById(R.id.collapsing_toolbar);
AppBarLayout appbar = (AppBarLayout)findViewById(R.id.app_bar_layout);
tool.setTitle("");
setSupportActionBar(tool);
c.setTitleEnabled(false);

appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {

    boolean isVisible = true;
    int scrollRange = -1;
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (scrollRange == -1) {
            scrollRange = appBarLayout.getTotalScrollRange();
        }
        if (scrollRange + verticalOffset == 0) {
           tool.setTitle("Title");
            isVisible = true;
        } else if(isVisible) {
            tool.setTitle("");
            isVisible = false;
        }
    }
});

答案 2 :(得分:3)

我今天发现了同样的问题。我终于通过在xml

上定义它来解决它
app:expandedTitleTextAppearance="@android:color/transparent"

另外,以编程方式检查pre-kitkat

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
    mCollapsingToolbar.setExpandedTitleTextAppearance(R.style.TransparentText)
};

TransparentText样式

<style name="TransparentText" parent="@android:style/TextAppearance">
       <item name="android:textColor">#00000000</item>
</style>

我知道这已经太晚了,但我希望这可以帮助其他人:)