美文网首页
通过反射改变toolbar

通过反射改变toolbar

作者: i冰点 | 来源:发表于2016-11-10 17:44 被阅读289次

1、改变toolbar的颜色

当滚动条再顶部的时候,toobar透明,再发生滚动的时候,toobar逐渐变成白色,效果如下:

toobar变色

关于toobar

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#00ffffff"
        app:elevation="0dp"
        app:navigationIcon="@drawable/back_white"/>

其中background设为白色全透,navigationIcon图片也是白色

实现这个效果,有三点需要注意:

1、获得toobar下的navigationIcon(mNavButtonView)、title(mTitleTextView)

反射

    private void getToolBarView() {
        Class<? extends Toolbar> c = toolbar.getClass();
        try {
            Field field=c.getDeclaredField("mNavButtonView");
            Field fieldTextView=c.getDeclaredField("mTitleTextView");
            field.setAccessible(true);
            fieldTextView.setAccessible(true);
            Object obj = null;//拿到对应的Object
            Object objTextView = null;//拿到对应的Object
            try {
                obj = field.get(toolbar);
                if(obj ==null)return;
                if(obj instanceof ImageButton) {
                    mNavButtonView = (ImageButton) obj;
                    mNavButtonView.setImageTintMode(PorterDuff.Mode.ADD);
                }
                objTextView=fieldTextView.get(toolbar);
                if(objTextView==null) return;
                if(objTextView instanceof TextView){
                    mTitleTextView= (TextView) objTextView;
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
2、改变navigationIcon的颜色

在布局文件可以使用android:tint,在代码中对应的方法是imageview.setColorFilter(Color)。

3、动态改变toobar的背景色
    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            //totalDis,再y轴移动的距离
            totalDis+=dy;
            //dis=header的高度(gif中绿色的那一块)-toobar的高度
            offset=totalDis*1.0f/dis;
            if(totalDis>=dis){
                offset=1;
            }
            //动态改变toobar的背景色
            toolbar.setBackgroundColor(Color.argb((int) (offset * 255), 255, 255, 255));
            mNavButtonView.setColorFilter(Color.argb(255, (int) ((1 - offset) * 255), (int) ((1 - offset) * 255), (int) ((1 - offset) * 255)));
            if(mTitleTextView!=null){
                mTitleTextView.setTextColor(Color.argb(255, (int) ((1-offset) * 255),  (int) ((1-offset) * 255),  (int) ((1-offset) * 255)));
            }
        }
    });

</br>
参考:
MaterialDesign文字缩放并入Toolbar效果的一种实现使用Material Design Tint和视图详解

</br>

2、toolbar居中

1、在XML中设置

toolbar的title默认是居左的,可以在toolbar里放置一个居中的子view,如下:

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:contentInsetStart="0dp"
        app:title="">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:text="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"/>
        </FrameLayout>
    </android.support.v7.widget.Toolbar>

注意:contentInsetStart默认是16dp,这里在布局文件中将contentInsetStart修改为0dp

没有设置contentInsetStart 设置了contentInsetStart

默认contentInsetStart=16dp,在样式Base.Widget.AppCompat.Toolbar中查看:

toobar的默认样式

也可以在配置文件中修改contentInsetStart,如下:


    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        ...
        <item name="toolbarStyle">@style/myToolBar</item>
    </style>

    <style name="myToolBar" parent="Base.Widget.AppCompat.Toolbar">
        <item name="contentInsetStart">0dp</item>
    </style>
2、自定义toolbar

Android官方Toolbar自定义高度最靠谱的解决办法

相关文章

网友评论

      本文标题:通过反射改变toolbar

      本文链接:https://www.haomeiwen.com/subject/tlexpttx.html