我们先上两张图,很容易看出区别:
/** * 关于界面 * * @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,这样像
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/s003603u/article/details/47133723