我们先上两张图,很容易看出区别:
/** * 关于界面 * * @author SuS * @time 2015.07.29 */ public class AboutActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_about); setImmerseLayout(findViewById(R.id.common_back)); initBackButton(); setTitleBar(R.string.durian_about); } @Override protected void onResume() { super.onResume(); } @Override protected void onPause() { super.onPause(); } @Override protected void onDestroy() { super.onDestroy(); } }现在请注意setImmerseLayout()这个方法,这个方法是在BaseActivity中实现的
public class BaseActivity extends FragmentActivity { private static final String TAG = "BaseActivity"; ............... public void initBackButton() { ImageView backButton = (ImageView) this.findViewById(R.id.durian_back_image); backButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finishActivity(); } }); } protected void setImmerseLayout(View view) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = getWindow(); /*window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);*/ window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); int statusBarHeight = ScreenUtil.getStatusBarHeight(this.getBaseContext()); view.setPadding(0, statusBarHeight, 0, 0); } } public void finishActivity() { finish(); overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out); } public void setTitleBar(int id) { TextView tvName = (TextView) findViewById(R.id.durian_title_text); tvName.setText(id); } }
/** * 用于获取状态栏的高度。 使用Resource对象获取(推荐这种方式) * * @return 返回状态栏高度的像素值。 */ public static int getStatusBarHeight(Context context) { int result = 0; int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = context.getResources().getDimensionPixelSize(resourceId); } return result; }
<include android:id="@+id/common_back" layout="@layout/common_back" />
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/durian_head_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/common_top_bg" > <RelativeLayout android:layout_width="match_parent" android:layout_height="51dp" > <ImageView android:id="@+id/durian_back_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="18dp" android:padding="5dp" android:src="@drawable/btn_back_selector" /> <TextView android:id="@+id/durian_title_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textColor="@color/common_text_black" android:textSize="18sp" /> <ImageView android:id="@+id/durian_titlebar_image1" android:layout_width="51dp" android:layout_height="51dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:scaleType="centerInside" android:visibility="gone" /> <ImageView android:id="@+id/durian_titlebar_image2" android:layout_width="51dp" android:layout_height="51dp" android:layout_centerVertical="true" android:layout_toLeftOf="@id/durian_titlebar_image1" android:scaleType="centerInside" android:visibility="gone" /> </RelativeLayout> </FrameLayout>
protected void setImmerseLayout(View view) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = getWindow(); /*window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);*/ window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); /* int statusBarHeight = ScreenUtil.getStatusBarHeight(this.getBaseContext()); view.setPadding(0, statusBarHeight, 0, 0);*/ } }
总结:
基于以上的方法介绍,我们可以实现状态栏与导航栏以及状态栏与页面大背景的沉浸式体验。
其实上面也可以看出代码封装的一些技巧:如让我们所有的activity继承BaseActivity,这样像
Android 5.0 如何实现将布局的内容延伸到状态栏实?
补充:这个只是我刚开始搞这部分内容,肯定存在很多不足,也有很多问题没有考虑到,觉得可以参考的就拿去,觉得比较low的大神请放过
更新:android沉浸式状态栏实现优化(带软键盘)