1琴锭、改變toolbar的顏色
當(dāng)滾動條再頂部的時候欲芹,toobar透明蝌借,再發(fā)生滾動的時候昔瞧,toobar逐漸變成白色,效果如下:
toobar變色
關(guān)于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設(shè)為白色全透菩佑,navigationIcon圖片也是白色
實(shí)現(xiàn)這個效果硬爆,有三點(diǎn)需要注意:
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;//拿到對應(yīng)的Object
Object objTextView = null;//拿到對應(yīng)的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于置,在代碼中對應(yīng)的方法是imageview.setColorFilter(Color)。
3袜蚕、動態(tài)改變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;
}
//動態(tài)改變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效果的一種實(shí)現(xiàn)绢涡、使用Material Design Tint和視圖詳解
</br>
2牲剃、toolbar居中
1、在XML中設(shè)置
toolbar的title默認(rèn)是居左的雄可,可以在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默認(rèn)是16dp,這里在布局文件中將contentInsetStart修改為0dp
沒有設(shè)置contentInsetStart
設(shè)置了contentInsetStart
默認(rèn)contentInsetStart=16dp数苫,在樣式Base.Widget.AppCompat.Toolbar中查看:
toobar的默認(rèn)樣式
也可以在配置文件中修改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>