首页 > 代码库 > Android 中沉浸式状态栏实现

Android 中沉浸式状态栏实现

Android 中沉浸式状态栏实现方式如下

 

计算状态栏高度及调用沉浸式状态栏的相关API方法

 1 package com.example.status; 2 import android.annotation.TargetApi; 3 import android.app.Activity; 4 import android.content.Context; 5 import android.os.Build; 6 import android.view.View; 7 import android.view.Window; 8 import android.view.WindowManager; 9 public class ImmersedStatusbarUtils {10      11     /**12      * 在{@link Activity#setContentView}之后调用13      * 14      * @param activity15      *            要实现的沉浸式状态栏的Activity16      * @param titleViewGroup17      *            头部控件的ViewGroup,若为null,整个界面将和状态栏重叠18      */19     @TargetApi(Build.VERSION_CODES.KITKAT)20     public static void initAfterSetContentView(Activity activity,21             View titleViewGroup) {22         if (activity == null)23             return;24         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {25             Window window = activity.getWindow();26             window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);27         }28         if (titleViewGroup == null)29             return;30         // 设置头部控件ViewGroup的PaddingTop,防止界面与状态栏重叠31         int statusBarHeight = getStatusBarHeight(activity);32         titleViewGroup.setPadding(0, statusBarHeight, 0, 0);33  34     }35  36     /**37      * 获取状态栏高度38      * 39      * @param context40      * @return41      */42     private static int getStatusBarHeight(Context context) {43         int result = 0;44         int resourceId = context.getResources().getIdentifier(45                 "status_bar_height", "dimen", "android");46         if (resourceId > 0) {47             result = context.getResources().getDimensionPixelSize(resourceId);48         }49         return result;50     }51 }

 

调用实例,使用步骤一编写的相关方法。

 

 1 package com.example.status; 2   3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.Menu; 6 import android.view.View; 7 import android.view.Window; 8   9 public class MainActivity extends Activity {10  11     @Override12     protected void onCreate(Bundle savedInstanceState) {13         super.onCreate(savedInstanceState);14         requestWindowFeature(Window.FEATURE_NO_TITLE);15         setContentView(R.layout.activity_main);16         View topView = findViewById(R.id.lin);17         ImmersedStatusbarUtils.initAfterSetContentView(this, topView);18     }19  20     @Override21     public boolean onCreateOptionsMenu(Menu menu) {22         // Inflate the menu; this adds items to the action bar if it is present.23         getMenuInflater().inflate(R.menu.main, menu);24         return true;25     }26  27 }

 

对应的布局文件

 

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" > 6   7     <LinearLayout 8         android:id="@+id/lin" 9         android:layout_width="match_parent"10         android:layout_height="wrap_content"11         android:background="#ff123654" >12  13         <TextView14             android:id="@+id/title"15             android:layout_width="match_parent"16             android:layout_height="45.0dp"17             android:background="#ff123654"18             android:gravity="center"19             android:text="我是头部控件"20             android:textColor="#ffffffff" />21     </LinearLayout>22  23 </LinearLayout>

 

技术分享

 

Android 中沉浸式状态栏实现