安卓:让CollapsingToolbarLayout标题透明,以避免与搜索查看重叠 [英] Android: make CollapsingToolbarLayout title transparent to avoid overlap with SearchView

查看:861
本文介绍了安卓:让CollapsingToolbarLayout标题透明,以避免与搜索查看重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面对这说明了同样的问题<一href=\"http://stackoverflow.com/questions/30742878/android-collapsingtoolbarlayout-and-searchview-text-overlapping\">SO问题(如果我的搜索查看是开放的,我崩溃我的工具栏,两个重叠)。所以我想执行核定的答案,让我的头衔透明的,当它处于折叠状态。解决办法只有部分工作对我来说,因为我有在消息ñ描述了同样的问题。 11这个bug 这里的报告。总之,如果在工具栏倒塌搜索查看已打开我的标题的颜色不回白色。这是我的布局:

 &LT; android.support.design.widget.CoordinatorLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent&GT;    &LT; android.support.design.widget.AppBarLayout
        机器人:layout_width =match_parent
        机器人:layout_height =@扪/ toolbar_expanded_height
        机器人:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar&GT;        &LT; android.support.design.widget.CollapsingToolbarLayout
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            应用:expandedTitleMarginEnd =64dp
            应用:expandedTitleMarginStart =70dp
            应用:layout_scrollFlags =滚动| exitUntilCollapsed&GT;            &LT; android.support.v7.widget.Toolbar
                机器人:layout_width =match_parent
                机器人:layout_height =?ATTR / actionBarSize
                应用:popupTheme =@风格/ ToolbarPopupTheme
                应用:layout_collapseMode =针/&GT;
        &LT; /android.support.design.widget.CollapsingToolbarLayout>
    &LT; /android.support.design.widget.AppBarLayout>
    ...
&LT; /android.support.design.widget.CoordinatorLayout>

这是code,我写信给听搜索查看打开和关闭:

 公共无效onCreateOptionsMenu(菜单菜单,MenuInflater气筒){
    super.onCreateOptionsMenu(菜单,充气器);
    inflater.inflate(R.menu.my_menu,菜单);
    菜单项searchItem = menu.findItem(R.id.search);
    MenuItemCompat.setOnActionExpandListener(searchItem,新MenuItemCompat.OnActionExpandListener(){
        @覆盖
        公共布尔onMenuItemActionExpand(菜单项项){
            collapsingToolbar.setCollapsedTitleTextColor(Color.TRANSPARENT);
            返回true;
        }        @覆盖
        公共布尔onMenuItemActionCollapse(菜单项项){
            collapsingToolbar.setCollapsedTitleTextColor(Color.WHITE);
            返回true;
        }
    });
    ...
}

其中, collapsingToolbar 显然是引用我CollapsingToolbarLayout。谢谢大家的时间。


解决方案

  MenuItemCompat.setOnActionExpandListener(menu​​.findItem(R.id.action_search),新MenuItemCompat.OnActionExpandListener(){
        @覆盖
        公共布尔onMenuItemActionExpand(菜单项项){
            mCollapsingToolbar.setCollapsedTitleTextColor(Color.TRANSPARENT);
            返回true;
        }        @覆盖
        公共布尔onMenuItemActionCollapse(菜单项项){
            mCollapsingToolbar.setCollapsedTitleTextColor(Color.WHITE);
            mCollapsingToolbar.setTitle(mCategoryName);
            返回false;
        }
    });

这正常工作对我。感谢您的previous帖子

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.

解决方案

 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

这篇关于安卓:让CollapsingToolbarLayout标题透明,以避免与搜索查看重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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