Android: make CollapsingToolbarLayout title transparent to avoid overlap with SearchView

南笙酒味 提交于 2019-12-14 04:00:55

问题


I'm facing the same problem described in this SO question (if my searchview is open and I collapse my toolbar, the two overlaps). So I was trying to implement the approved answer, making my title transparent when it is collapsed. Solution is only partially working for me, because I'm having the same problem described in the message n. 11 in the report of this bug here. In short, the color of my title doesn't return to white if the searchview is open when the toolbar is collapsed. This is my layout:

<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="@dimen/toolbar_expanded_height"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="70dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ToolbarPopupTheme"
                app:layout_collapseMode="pin" />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
    ...
</android.support.design.widget.CoordinatorLayout>

And this is the code I've wrote to listen searchview open and close:

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.my_menu, menu);
    MenuItem searchItem = menu.findItem(R.id.search);
    MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            collapsingToolbar.setCollapsedTitleTextColor(Color.TRANSPARENT);
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            collapsingToolbar.setCollapsedTitleTextColor(Color.WHITE);
            return true;
        }
    });
    ...
}

where collapsingToolbar is obviously the reference to my CollapsingToolbarLayout. Thank you all for your time.


回答1:


 MenuItemCompat.setOnActionExpandListener(menu.findItem(R.id.action_search), new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            mCollapsingToolbar.setCollapsedTitleTextColor(Color.TRANSPARENT);
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            mCollapsingToolbar.setCollapsedTitleTextColor(Color.WHITE);
            mCollapsingToolbar.setTitle(mCategoryName);
            return false;
        }
    });

This works fine for me. Thanks for your previous posts



来源:https://stackoverflow.com/questions/32936050/android-make-collapsingtoolbarlayout-title-transparent-to-avoid-overlap-with-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!