最近这几天在捉摸沉浸式bar,然后自己研究探索出来一种方法,写出来分享一下,大家有什么见解可以和我一起交流哦!
话不多说直接上效果图
xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.jingcai.fu.kuangjia.application.BaseActivity"> <include layout="@layout/for_head"/>--这是状态栏 <include layout="@layout/for_common_bar"/> ---这是替代的actionBar的布局 这两个布局会在下面给出 <RelativeLayout -------这是你所需要的主布局 android:layout_weight="1" android:layout_width="match_parent" android:id="@+id/main_container" android:layout_height="match_parent"> </RelativeLayout> </LinearLayout>
1:将你activity的theme设置这个属性
<item name="android:windowTranslucentStatus">true</item>
2:隐藏actionBar
getSupportActionBar().hide();
3:设置状态栏的高度
/**设置状态栏的高度*/ public void setScreenTop(){ FrameLayout view = (FrameLayout) findViewById(R.id.phone_title); ViewGroup.LayoutParams lp = view.getLayoutParams(); lp.height = getStatusHeight(this);--具体方法在下面 view.setLayoutParams(lp); }这是状态栏的xml布局,就是手机屏幕最上面的布局/** * 获得状态栏的高度 * * @param context * @return */ public static int getStatusHeight(Context context) { int statusHeight = -1; try { Class<?> clazz = Class.forName("com.android.internal.R$dimen"); Object object = clazz.newInstance(); int height = Integer.parseInt(clazz.getField("status_bar_height") .get(object).toString()); statusHeight = context.getResources().getDimensionPixelSize(height); } catch (Exception e) { e.printStackTrace(); } return statusHeight; }
for_head.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/phone_title" android:layout_width="match_parent" android:layout_height="40dp" android:background="@color/colorPrimaryDark" tools:context="com.jingcai.fu.kuangjia.fragment.main.FirstFragment"> <FrameLayout android:id="@+id/childView" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> </FrameLayout>这就把状态栏设置好了 ,你想要什么颜色就可以设置
给一张效果图
设置好后的是这样的
然后设置你的actionBar的布局
for_common_bar的布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="horizontal" android:id="@+id/bar" android:background="@color/colorPrimary" android:layout_height="60dp"> <!--android:src="@drawable/icon_back"--> <ImageView android:layout_margin="@dimen/five" android:gravity="center_vertical" android:src="@drawable/icon_back" android:layout_width="55dp" android:layout_height="55dp" android:id="@+id/common_back"/> <TextView android:layout_toRightOf="@+id/common_back" android:textColor="@color/font_white" android:textSize="@dimen/font_huge" android:layout_marginLeft="@dimen/five" android:gravity="center_vertical" android:text="@string/app_chinese_name" android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/activity_name"/> <ImageView android:layout_marginRight="@dimen/three" android:layout_alignParentRight="true" android:gravity="center_vertical" android:src="@mipmap/banner_01" android:layout_width="55dp" android:layout_height="55dp" android:id="@+id/common_icon"/> </RelativeLayout>
然后就出来这样的效果了 是不是很简单吧
后记------我写了BaseActivity然后其他继承这个activity
我把代码贴出来你们可以看一下
package com.jingcai.fu.kuangjia.application; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import com.jingcai.fu.kuangjia.R; import com.jingcai.fu.kuangjia.utils.ScreenUtils; public class BaseActivity extends AppCompatActivity { private TextView title; private ImageView rightImageView,back; private View bar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().hide(); setContentView(R.layout.activity_base); setScreenTop(); bar = findViewById(R.id.bar); title = (TextView) findViewById(R.id.activity_name); rightImageView = (ImageView) findViewById(R.id.common_icon); back = (ImageView)findViewById(R.id.common_back); hideCommonBar(); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } /** * 设置title */ public void setTitle(String string) { if (string != null) title.setText(string); } /** * 设置状态栏的高度 */ public void setScreenTop() { FrameLayout view = (FrameLayout) findViewById(R.id.phone_title); ViewGroup.LayoutParams lp = view.getLayoutParams(); lp.height = ScreenUtils.getStatusHeight(this); view.setLayoutParams(lp); } /** * 设置布局 */ public void baseSetContentView(int layoutResID) { RelativeLayout container = (RelativeLayout) findViewById(R.id.main_container); LayoutInflater inflater = getLayoutInflater(); View v = inflater.inflate(layoutResID, null); container.addView(v); } /** * 隐藏bar */ public void hideCommonBar() { bar.setVisibility(View.GONE); } /** * rightImageView设置src */ public void setRightImageViewSrc(Integer ImgResId) { rightImageView.setImageResource(ImgResId); } /** * backImageView设置src */ public void setBackImageViewSrc(Integer ImgResId) { back.setImageResource(ImgResId); } }