Android沉浸式顶栏的实现

来源:互联网 发布:傲梦少儿编程怎样 编辑:程序博客网 时间:2024/05/16 15:33

最近好长时间没用path了,今天登陆之后猛然间发现path的的导航条和手机系统的通知栏的颜色融为一体,咨询过别人之后才发现这是Android4.4系统出现的新功能,官方术语叫:沉浸式顶栏。现在我们就来记录该功能的实现方式:

首先创建一个Android项目。



在style.xml中添加


   <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中添加

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()));        
<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);        }
<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文件夹中添加

<?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更改为上边我们定义的样式

<?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 0