android沉浸式状态栏的实现

    xiaoxiao2026-03-16  7

    在style.xml中添加

    [html]  view plain copy <style name="Theme.Timetodo" parent="@android:style/Theme.Holo.Light">           <!-- translucent system bars -->        <item name="android:windowTranslucentStatus">true</item>        <item name="android:windowTranslucentNavigation">true</item>    </style>  

    其中 android:windowTranslucentStatus表示是否要填充顶部的状态栏区域android:windowTranslucentNavigation表示是否要填充底部的状态栏区域这两种样式的目的就是默认让应用的内容放置到系统栏的下边,如果仅仅想扩展背景样式到系统栏下边,则需要设置android:fitsSystemWindows为true,会增加试图的Pading值让你的布局恢复正常大小,并且可以将背景扩大。

    在已经创建的Activity中添加

    [java]  view plain copy package com.example.androidedemo;      import java.lang.reflect.Field;      import android.annotation.SuppressLint;   import android.app.ActionBar;   import android.app.ActionBar.LayoutParams;   import android.app.Activity;   import android.content.res.Resources;   import android.graphics.Color;   import android.graphics.drawable.Drawable;   import android.os.Bundle;   import android.util.TypedValue;   import android.view.Menu;   import android.view.View;   import android.view.ViewGroup;   import android.view.Window;   import android.view.WindowManager;   import android.widget.LinearLayout;   import android.widget.ListView;   import android.widget.RelativeLayout;   import android.widget.TextView;      @SuppressLint("NewApi")   public class MainActivity extends Activity {       private RelativeLayout rlLayout;          @Override       protected void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.activity_main);           //设置ACtionBar           ActionBar actionBar = getActionBar();           Resources r = getResources();           Drawable myDrawable = r.getDrawable(R.drawable.ba);           actionBar.setBackgroundDrawable(myDrawable);                      actionBar.setDisplayHomeAsUpEnabled(true);           actionBar.setHomeButtonEnabled(true);                      rlLayout = (RelativeLayout) findViewById(R.id.rlayout);           ListView listView = (ListView) findViewById(R.id.listView);           listView.setAdapter(new MyAdapter(getApplicationContext()));              [java]  view plain copy <span style="white-space:pre">    </span>//此处判断的目的是让Android系统大于等于4.4的系统才执行沉浸式的功能           if (android.os.Build.VERSION.SDK_INT > 18) {               Window window = getWindow();               window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);               window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);           }   [java]  view plain copy <span style="white-space:pre">    </span>//获取到系统通知栏的高度,然后给系统通知栏设置我们需要的颜色。并将其addView到ViewGroup中。           // 创建TextView            TextView textView = new TextView(this);            LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, getStatusBarHeight());            textView.setBackgroundColor(Color.parseColor("#3F9FE0"));            textView.setLayoutParams(lParams);            // 获得根视图并把TextView加进去。            ViewGroup view = (ViewGroup) getWindow().getDecorView();            view.addView(textView);        }             //开启全屏模式       @SuppressLint("NewApi")       public static void hideSystemUI(View view) {           view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE                   | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN                   | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION                   | View.SYSTEM_UI_FLAG_FULLSCREEN                   | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);       }          //取消全屏模式       @SuppressLint("NewApi")       public static void showSystemUI(View view) {           view.setSystemUiVisibility(                   View.SYSTEM_UI_FLAG_LAYOUT_STABLE                   | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);       }           // 获取手机状态栏高度       public int getStatusBarHeight() {           Class<?> c = null;           Object obj = null;           Field field = null;           int x = 0, statusBarHeight = 0;           try {               c = Class.forName("com.android.internal.R$dimen");               obj = c.newInstance();               field = c.getField("status_bar_height");               x = Integer.parseInt(field.get(obj).toString());               statusBarHeight = getResources().getDimensionPixelSize(x);           } catch (Exception e1) {               e1.printStackTrace();           }           return statusBarHeight;       }          // 获取ActionBar的高度       public int getActionBarHeight() {           TypedValue tv = new TypedValue();           int actionBarHeight = 0;           if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))// 如果资源是存在的、有效的           {               actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());           }           return actionBarHeight;       }   }  

    在drawable文件夹中添加

    [html]  view plain copy <?xml version="1.0" encoding="utf-8"?>   <shape xmlns:android="http://schemas.android.com/apk/res/android" >          <gradient           android:angle="270"           android:endColor="#c8c8c8"           android:startColor="#3F9FE0"           android:type="linear" />      </shape>    

    此代码是给您的导航条设置一个渐变,目的是让导航条和系统通知栏的样式融合看起来更加紧密。

    最后在AndroidManifest.xml文件中将Application中的theme更改为上边我们定义的样式

    [html]  view plain copy <?xml version="1.0" encoding="utf-8"?>   <manifest xmlns:android="http://schemas.android.com/apk/res/android"       package="com.example.androidedemo"       android:versionCode="1"       android:versionName="1.0" >          <uses-sdk           android:minSdkVersion="8"           android:targetSdkVersion="8" />          <application           android:allowBackup="true"           android:icon="@drawable/ic_launcher"           android:label="@string/app_name"           android:theme="@style/Theme.Timetodo" >           <activity               android:name="com.example.androidedemo.MainActivity"               android:label="@string/app_name" >               <intent-filter>                   <action android:name="android.intent.action.MAIN" />                      <category android:name="android.intent.category.LAUNCHER" />               </intent-filter>           </activity>       </application>      </manifest>  
    最新回复(0)