自定义actionbar布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/action_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/rl_actionbar_background"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/black_000000">
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_action_bar"
android:layout_width="match_parent"
android:layout_height="48dp">
<ImageView
android:id="@+id/actionbar_left_icon"
android:layout_width="24dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/margin_mini"
android:src="@mipmap/guanliqi_back"
android:visibility="visible" />
<TextView
android:id="@+id/actionbar_left_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:paddingLeft="@dimen/margin_mini"
android:textColor="@color/gray_999"
android:textSize="@dimen/nav_text_right_size" />
<LinearLayout
android:id="@+id/actionbar_center_layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="@dimen/margin_wee"
android:paddingRight="@dimen/margin_wee">
<TextView
android:id="@+id/actionbar_center_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:textColor="@color/white"
android:textSize="@dimen/nav_text_center_size" />
<ImageView
android:id="@+id/actionbar_center_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_mini" />
</LinearLayout>
<ImageView
android:id="@+id/actionbar_right_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/actionbar_right_text"
android:layout_toStartOf="@+id/actionbar_right_text" />
<TextView
android:id="@+id/actionbar_right_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:paddingRight="@dimen/margin_mini"
android:textColor="@color/gray_999"
android:textSize="@dimen/nav_text_right_size" />
</RelativeLayout>
</RelativeLayout>
“@+id/rl_actionbar_background”此id的relativelayout作为自定义actionbar的背景,默认为黑色。
正题才刚刚开始
要实现监听scrollview的滑动从而实现自定义actionbar的透明度渐变的效果,需要用到上一篇文章,然后在fragment或者activity里面来实现对scrollview的监听
方案一
第一步:初始化actionbar背景布局透明度为0(因为是单独一个布局做了背景,0为全透明)
actionbarBackground.setAlpha(0);
第二步:
监听“我的”界面的scrollview,实现自定义actionbar背景透明度的变化
@Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
if (y <= 250) {
float scale = (float) y / 250;
//改变一个布局的透明度
actionbarBackground.setAlpha(scale);
}else {
actionbarBackground.setAlpha(1);
}
}
方案二
方案一里是直接给actionbar设置了一个单独的背景布局,然后通过改变该背景布局的透明度来实现的,方案二将通过直接改变actionbar的背景透明度(而不用再单独写一个背景布局了)
第一步//初始化布局的背景透明度为0(全透明),颜色为黑
actionbarBackground.setBackgroundColor(Color.argb(0, 0, 0, 0));
第二步
//actionbar透明度渐变
@Override
public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
if (y <= 250) {
float scale = (float) y / 250;
float alpha = (255 * scale);
//改变一个布局的背景的透明度
actionbarBackground.setBackgroundColor(Color.argb((int) alpha, 0, 0, 0));
}else {
actionbarBackground.setBackgroundColor(Color.argb(255, 0, 0, 0));
}
}