国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程中沉浸式狀態欄的三種實現方式詳解

2019-10-22 18:15:33
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程中沉浸式狀態欄的三種實現方式。分享給大家供大家參考,具體如下:

沉浸式狀態欄

Google從android kitkat(Android 4.4)開始,給我們開發者提供了一套能透明的系統ui樣式給狀態欄和導航欄,這樣的話就不用向以前那樣每天面對著黑乎乎的上下兩條黑欄了,還可以調成跟Activity一樣的樣式,形成一個完整的主題,和IOS7.0以上系統一樣了。

首先看下效果

Android,沉浸式狀態欄

首先看下第一種方式

系統的方式沉浸式狀態欄實現

步奏一

//當系統版本為4.4或者4.4以上時可以使用沉浸式狀態欄if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {      //透明狀態欄      getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);      //透明導航欄    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);}

步奏二

布局加入:

android:fitsSystemWindows="true"android:clipToPadding="true"

我們看下activity和布局文件

FirstActivity.java:

/*** 沉浸式狀態欄*/private void initState() {    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {      //透明狀態欄      getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);      //透明導航欄      getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);    }}

activity_first.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent">  <TextView    android:fitsSystemWindows="true"    android:clipToPadding="true"    android:layout_width="match_parent"    android:layout_height="140dp"    android:textSize="24dp"    android:background="@color/mask_tags_1"    android:text="你好,沉浸式狀態欄"/></LinearLayout>

接著看下第二種方式

實現思路,添加隱藏布局,然后我們動態的計算狀態欄的高度,然后把這個高度設置成這個隱藏的布局的高度,便可以實現

在這里我們通過反射來獲取狀態欄的高度

/*** 通過反射的方式獲取狀態欄高度** @return*/private int getStatusBarHeight() {    try {      Class<?> c = Class.forName("com.android.internal.R$dimen");      Object obj = c.newInstance();      Field field = c.getField("status_bar_height");      int x = Integer.parseInt(field.get(obj).toString());      return getResources().getDimensionPixelSize(x);    } catch (Exception e) {      e.printStackTrace();    }    return 0;}

來看下SecondActivity和布局文件吧

SecondActivity.java

package com.example.translucentbarstest;import android.os.Build;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.WindowManager;import android.widget.LinearLayout;import java.lang.reflect.Field;/** * Created by 若蘭 on 2016/1/22. * 一個懂得了編程樂趣的小白,希望自己 * 能夠在這個道路上走的很遠,也希望自己學習到的 * 知識可以幫助更多的人,分享就是學習的一種樂趣 * QQ:1069584784 */public class SecondActivity extends AppCompatActivity {  @Override  protected void onCreate(@Nullable Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_two);    initState();  }  /**   * 動態的設置狀態欄 實現沉浸式狀態欄   *   */  private void initState() {    //當系統版本為4.4或者4.4以上時可以使用沉浸式狀態欄    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {      //透明狀態欄      getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);      //透明導航欄      getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);      //      LinearLayout linear_bar = (LinearLayout) findViewById(R.id.ll_bar);      linear_bar.setVisibility(View.VISIBLE);      //獲取到狀態欄的高度      int statusHeight = getStatusBarHeight();      //動態的設置隱藏布局的高度      LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) linear_bar.getLayoutParams();      params.height = statusHeight;      linear_bar.setLayoutParams(params);    }  }  /**   * 通過反射的方式獲取狀態欄高度   *   * @return   */  private int getStatusBarHeight() {    try {      Class<?> c = Class.forName("com.android.internal.R$dimen");      Object obj = c.newInstance();      Field field = c.getField("status_bar_height");      int x = Integer.parseInt(field.get(obj).toString());      return getResources().getDimensionPixelSize(x);    } catch (Exception e) {      e.printStackTrace();    }    return 0;  }}

activity_second.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical"  tools:context="com.example.translucentbarstest.TwoActivity">  <!--這個是隱藏的布局,然后通過動態的設置高度達到效果-->  <LinearLayout    android:id="@+id/ll_bar"    android:layout_width="fill_parent"    android:layout_height="1dp"    android:orientation="vertical"    android:background="#e7abff"    android:visibility="gone">  </LinearLayout>  <TextView    android:fitsSystemWindows="true"    android:clipToPadding="true"    android:layout_width="match_parent"    android:layout_height="140dp"    android:background="@color/mask_tags_3"    android:text="你好,沉浸式狀態欄"/></LinearLayout>

接下來看下第三種

這個是用的github上的第三方庫

1.庫地址:https://github.com/jgilfelt/SystemBarTint

2.添加依賴庫:

compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'

步奏一

android:fitsSystemWindows="true"android:clipToPadding="true

步奏二

SystemBarTintManager tintManager = new SystemBarTintManager(this);// 激活狀態欄tintManager.setStatusBarTintEnabled(true);// enable navigation bar tint 激活導航欄tintManager.setNavigationBarTintEnabled(true);//設置系統欄設置顏色//tintManager.setTintColor(R.color.red);//給狀態欄設置顏色tintManager.setStatusBarTintResource(R.color.mask_tags_1);//Apply the specified drawable or color resource to the system navigation bar.//給導航欄設置資源tintManager.setNavigationBarTintResource(R.color.mask_tags_1);

來看下代碼吧

ThreeActivity.java

package com.example.translucentbarstest;import android.os.Build;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.WindowManager;import com.readystatesoftware.systembartint.SystemBarTintManager;/** * Created by 若蘭 on 2016/1/22. * 一個懂得了編程樂趣的小白,希望自己 * 能夠在這個道路上走的很遠,也希望自己學習到的 * 知識可以幫助更多的人,分享就是學習的一種樂趣 * QQ:1069584784 */public class ThreeActivity extends AppCompatActivity{  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_three);    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {      //透明狀態欄      getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);      //透明導航欄      getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);      SystemBarTintManager tintManager = new SystemBarTintManager(this);      // 激活狀態欄      tintManager.setStatusBarTintEnabled(true);      // enable navigation bar tint 激活導航欄      tintManager.setNavigationBarTintEnabled(true);      //設置系統欄設置顏色      //tintManager.setTintColor(R.color.red);      //給狀態欄設置顏色      tintManager.setStatusBarTintResource(R.color.mask_tags_1);      //Apply the specified drawable or color resource to the system navigation bar.      //給導航欄設置資源      tintManager.setNavigationBarTintResource(R.color.mask_tags_1);    }  }}

activity_three.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:background="#ffff"  android:orientation="vertical"  tools:context="com.example.translucentbarstest.ThirdActivity">  <TextView    android:layout_width="match_parent"    android:layout_height="140dp"    android:background="@color/mask_tags_5"    android:clipToPadding="true"    android:fitsSystemWindows="true"    android:text="你好,沉浸式狀態欄"    android:textSize="24dp"/></LinearLayout>

好了,原來自己以為沉浸式狀態欄聽著好厲害(有可能自己原先不知道),但是真正自己去做了,去了解了,也沒有那么難、那么神秘了,我想這也是自己成長了一些。
繼續努力。這個是上傳的github上的demo: https://github.com/wuyinlei/-

希望本文所述對大家Android程序設計有所幫助。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 伊宁市| 临颍县| 伊宁市| 丰原市| 章丘市| 利川市| 宝清县| 太康县| 浠水县| 肇州县| 绥宁县| 玛多县| 保德县| 鄢陵县| 曲麻莱县| 信宜市| 普陀区| 会理县| 陆良县| 建平县| 卢龙县| 体育| 南丹县| 广昌县| 疏勒县| 大丰市| 成都市| 西宁市| 二连浩特市| 交口县| 新和县| 双鸭山市| 綦江县| 溆浦县| 黄冈市| 卢龙县| 富川| 神农架林区| 华宁县| 青海省| 辽阳市|