需求
全面屏效果,透明状态栏和toolbar.滑动后toolbar出现,状态栏变色.
其实最简单的方式可以使用coordinatorlayout,我想用代码实现所以...说代码吧:
布局准备
备注:LivePosterIv是一个自定义的imageview,依照你自己的需求替换就行
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/out_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.izxjf.liao.conferencelive.utils.MyScrollview2
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/bg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/rl_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<com.izxjf.liao.conferencelive.utils.LivePosterIv
android:id="@+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/live_default_bg"
app:live_height="0.6" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/m_small_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="@color/white"
android:textSize="@dimen/s18"
android:textStyle="bold" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="@+id/tv_label_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="5dp"
android:text="111"
android:textColor="@color/white"
android:textSize="@dimen/s13" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:text="内容"
android:textColor="@color/white"
android:textSize="@dimen/s13" />
<View
android:layout_width="1dip"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="@color/white" />
<TextView
android:id="@+id/tv_label_people"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="111"
android:textColor="@color/white"
android:textSize="@dimen/s13" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="人关注"
android:textColor="@color/white"
android:textSize="@dimen/s13" />
</LinearLayout>
<ImageView
android:id="@+id/button_big_guanzhu_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginLeft="20dp"
android:visibility="visible"
android:background="@drawable/button_big_guanzhu_yes"
/>
<ImageView
android:id="@+id/button_big_guanzhu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:visibility="gone"
android:layout_marginLeft="20dp"
android:background="@drawable/button_big_guanzhu"
/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<com.izxjf.liao.baselibrary.view.recycler_view.WrapRecyclerView
android:id="@+id/news_list_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg"
android:overScrollMode="never" />
<include
layout="@layout/common_no_data_layout"
android:visibility="gone" />
<include
layout="@layout/common_no_net_layout"
android:visibility="gone" />
</RelativeLayout>
</LinearLayout>
</com.izxjf.liao.conferencelive.utils.MyScrollview2>
</com.scwang.smartrefresh.layout.SmartRefreshLayout
>
<RelativeLayout
android:id="@+id/back_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/title_bar_height"
android:layout_alignParentTop="true"
android:background="@color/white"
android:gravity="center_vertical">
<RelativeLayout
android:id="@+id/back"
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_marginLeft="15dp">
<ImageView
android:id="@+id/back_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/white_back" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:id="@+id/m_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="@dimen/s16"
android:textStyle="bold" />
<ImageView
android:id="@+id/m_title_guanzhu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/m_title"
android:visibility="visible"
android:background="@drawable/button_guanzhu"
/>
<ImageView
android:id="@+id/m_title_guanzhu_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/m_title"
android:visibility="gone"
android:background="@drawable/button_guanzhu_yes"
/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
代码实现
1.初始化
@Override
protected void setContentView() {
setContentView(R.layout.activity_label);
x.view().inject(this);
int statusBarHeight = getStatusBarHeight(this);//工具类内方法
titleGuanzhu.getBackground().mutate().setAlpha(0);//不想显示的都设置透明
titleGuanzhu_yes.getBackground().mutate().setAlpha(0);
back_text.setTextColor(Color.argb((int) 0, 0, 0, 0)); //文字透明度((int) (alpha * 255));
toolbar.getBackground().mutate().setAlpha(0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 84);
layoutParams.setMargins(0, statusBarHeight, 0, 0);
toolbar.setLayoutParams(layoutParams);
}
提供一个状态栏工具类,可以写在baseActivity内
public class StatusBarUtils {
public static void setStatusBarColor(Activity activity , int color){
Window window = activity.getWindow();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
//设置Flags 适配手机
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(color);
}else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
//首先先设置全屏
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//获取decorView
ViewGroup decorView = (ViewGroup) window.getDecorView();
//获取contentView
View statusBar = decorView.findViewById(android.R.id.content);
//创建View
View view = new View(activity);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,getStatusBarHeight(activity)));
statusBar.setBackgroundColor(color);
decorView.addView(statusBar);
ViewGroup activityView = (ViewGroup) activity.findViewById(android.R.id.content);
activityView.getChildAt(0).setFitsSystemWindows(true);
}
}
public static void setActivityTranslucent(Activity activity){
Window window = activity.getWindow();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
//获取decorView
ViewGroup decorView = (ViewGroup) window.getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
}else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
//设置全屏
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
public static void closeActivityTranslucent(Activity activity){
Window window = activity.getWindow();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
//设置Flags 适配手机
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.BLACK);
}else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
//首先先设置全屏
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//获取decorView
ViewGroup decorView = (ViewGroup) window.getDecorView();
//获取contentView
View statusBar = decorView.findViewById(android.R.id.content);
//创建View
View view = new View(activity);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,getStatusBarHeight(activity)));
statusBar.setBackgroundColor(Color.BLACK);
decorView.addView(statusBar);
ViewGroup activityView = (ViewGroup) activity.findViewById(android.R.id.content);
activityView.getChildAt(0).setFitsSystemWindows(true);
}
}
//获取状态栏高度
public static int getStatusBarHeight(Activity activity) {
// 插件式换肤:怎么获取资源的,先获取资源id,根据id获取资源
Resources resources = activity.getResources();
int statusBarHeightId = resources.getIdentifier("status_bar_height","dimen","android");
return resources.getDimensionPixelOffset(statusBarHeightId);
}
}
2.根据scrollerview滚动改变toolbar样式,card是那张大大的背景图,我要获取他的高度所以要用post方法.
card.post(new Runnable() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void run() {
height = card.getMeasuredHeight();
scrollview.setScrollViewListener(new MyScrollview2.ScrollViewListener() {
@Override
public void onScrollChanged(MyScrollview2 scrollView, int l, int t, int oldl, int oldt) {
int mTransEndY = height;
int mTransViewHeight = toolbar.getMeasuredHeight();
if (t > height / 2) {
back_text.setAlpha(1);
back_text.setTextColor(Color.argb((int) (1 * 255), 0, 0, 0)); //文字透明度((int) (alpha * 255));
mBackImage.setImageResource(R.drawable.bigback);
titleGuanzhu.getBackground().mutate().setAlpha((int) (1 * 255));
titleGuanzhu_yes.getBackground().mutate().setAlpha((int) (1 * 255));
toolbar.getBackground().mutate().setAlpha((int) (1 * 255));
//不设置背景色就是透明的了
setStatusBarColor(Color.argb((int) (1 * 255), 255, 255, 255));
StatusBarUtil.StatusBarLightMode(LabelActivity.this);
}
if (t < height / 2) {
setStatusBarColor(Color.argb((int) (0 * 255), 0, 0, 0));
back_text.setAlpha(0);
back_text.setTextColor(Color.argb((int) (0 * 255), 0, 0, 0)); //文字透明度((int) (alpha * 255));
mBackImage.setImageResource(R.drawable.back_white);
titleGuanzhu.getBackground().mutate().setAlpha((int) (0 * 255));
titleGuanzhu_yes.getBackground().mutate().setAlpha((int) (0 * 255));
toolbar.getBackground().mutate().setAlpha((int) (0 * 255));
StatusBarUtil.transparencyBar(LabelActivity.this);
// m_right_tv.setTextColor(Color.WHITE;
}
if (mTransEndY == 0) {
return;
}
float alpha = (float) t / (mTransEndY - mTransViewHeight);
if (alpha <= 0) {
alpha = 0;
}
if (alpha > 1) {
alpha = 1;
}
}
});
}
});