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

首頁(yè) > 系統(tǒng) > Android > 正文

Android之沉浸式狀態(tài)欄的實(shí)現(xiàn)方法、狀態(tài)欄透明

2019-12-12 03:50:40
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

現(xiàn)在越來(lái)越多的軟件都開(kāi)始使用沉浸式狀態(tài)欄了,下面總結(jié)一下沉浸式狀態(tài)欄的兩種使用方法

注意!沉浸式狀態(tài)欄只支持安卓4.4及以上的版本

狀態(tài)欄:4.4上是漸變色,5.0上是完全透明,本文模擬器為4.4演示

效果圖:

這里寫(xiě)圖片描述

注意!兩種方法的區(qū)別:

第一種:為頂部欄跟隨當(dāng)前activity的布局文件的背景的顏色,使用方便,不過(guò)也有點(diǎn)問(wèn)題就是,如果有底部虛擬導(dǎo)航鍵的話(huà),導(dǎo)航鍵的背景跟頂部的顏色一樣,比如:

這里寫(xiě)圖片描述 

第二種:是通過(guò)設(shè)置頂部欄的顏色來(lái)顯示的,可以解決第一種的不足,比如:

這里寫(xiě)圖片描述

第一種使用方法:

第一、首先在values、values-v19、values-v21文件夾下的styles.xml都設(shè)置一個(gè) Translucent System Bar 風(fēng)格的Theme,如下圖:

這里寫(xiě)圖片描述

values/style.xml:

<style name="TranslucentTheme" parent="AppTheme">  <!--在Android 4.4之前的版本上運(yùn)行,直接跟隨系統(tǒng)主題--></style>values-v19/style.xml:<style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">  <item name="android:windowTranslucentStatus">true</item>  <item name="android:windowTranslucentNavigation">true</item></style>

values-v21/style.xml:

<style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">  <item name="android:windowTranslucentStatus">false</item>  <item name="android:windowTranslucentNavigation">true</item>  <!--Android 5.x開(kāi)始需要把顏色設(shè)置透明,否則導(dǎo)航欄會(huì)呈現(xiàn)系統(tǒng)默認(rèn)的淺灰色-->  <item name="android:statusBarColor">@android:color/transparent</item></style>

第二、在清單文件中配置需要沉浸式狀態(tài)欄的activity加入theme

<activity android:name=".ImageActivity" android:theme="@style/TranslucentTheme" /><activity android:name=".ColorActivity" android:theme="@style/TranslucentTheme" />

第三、在Activity的布局文件中的跟布局加入“android:fitsSystemWindows=”true””,但是,這里需要區(qū)分一下,就是背景是圖片還是純色:

1.當(dāng)背景為圖片時(shí),布局可以這么寫(xiě):

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:background="@drawable/imgs_bj"  android:fitsSystemWindows="true"></RelativeLayout>

效果:

2.當(dāng)背景為純色,我們需要對(duì)布局劃分一下,標(biāo)題布局與內(nèi)容布局,先把根布局背景設(shè)置成標(biāo)題布局的背景色,然后標(biāo)題背景色可以不用設(shè)置直接使用根布局的背景色,最后內(nèi)容布局背景色設(shè)置為白色

<?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"  android:background="@color/colorPrimary" //根布局背景設(shè)置成“標(biāo)題布局”想要的顏色  android:fitsSystemWindows="true"  android:orientation="vertical">  <!--標(biāo)題布局-->  <RelativeLayout    android:layout_width="match_parent"    android:layout_height="55dp"    android:background="@color/color_31c27c">    <TextView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_centerInParent="true"      android:text="這是標(biāo)題"      android:textColor="@android:color/white"      android:textSize="20sp" />  </RelativeLayout>  <!--內(nèi)容布局-->  <LinearLayout    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@android:color/white" //內(nèi)容區(qū)域背景設(shè)置成白色    android:gravity="center"    android:orientation="vertical">    <Button      android:layout_marginTop="120dp"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:padding="8dp"      android:text="顯示信息"      android:onClick="showMsg"      />  </LinearLayout></LinearLayout>

效果圖:

這里寫(xiě)圖片描述

好了,以上就是沉浸式狀態(tài)欄實(shí)現(xiàn)的全過(guò)程,但是還有一點(diǎn)值得注意的就是,如果我們activity比較多,每一個(gè)頁(yè)面都添加Android:fitsSystemWindows="true" 比較麻煩,我們需要改動(dòng)一下:

寫(xiě)一個(gè)基類(lèi)BaseColorActivity.class,代碼如下:

public abstract class BaseColorActivity extends AppCompatActivity {  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    //這一行注意!看本文最后的說(shuō)明!!!!    supportRequestWindowFeature(Window.FEATURE_NO_TITLE);    setContentView(getLayoutResId());//把設(shè)置布局文件的操作交給繼承的子類(lèi)    ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);    View parentView = contentFrameLayout.getChildAt(0);    if (parentView != null && Build.VERSION.SDK_INT >= 14) {      parentView.setFitsSystemWindows(true);    }  }  /**   * 返回當(dāng)前Activity布局文件的id   *   * @return   */  abstract protected int getLayoutResId();}

然后需要沉浸狀態(tài)欄的activity繼承該基類(lèi):

public class ColorActivity extends BaseColorActivity {  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    //這里不需要寫(xiě)setContentView()!  }  @Override  protected int getLayoutResId() {    //onCreate的方法中不需要寫(xiě)setContentView(),直接把當(dāng)前activity的布局文件在這里返回就行了!    return R.layout.activity_color;  }}

然后需要沉浸狀態(tài)欄的activity的布局文件中就可以把a(bǔ)ndroid:fitsSystemWindows="true"這行代碼給省略了!

第二種使用方法(未完):

這里寫(xiě)圖片描述 

寫(xiě)個(gè)工具類(lèi)StatusBarCompat.class:

public class StatusBarCompat {  private static final int INVALID_VAL = -1;  private static final int COLOR_DEFAULT = Color.parseColor("#20000000");  @TargetApi(Build.VERSION_CODES.LOLLIPOP)  public static void compat(Activity activity, int statusColor)  {    //當(dāng)前手機(jī)版本為5.0及以上     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)    {      if (statusColor != INVALID_VAL)      {        activity.getWindow().setStatusBarColor(statusColor);      }      return;    }    //當(dāng)前手機(jī)版本為4.4    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)    {      int color = COLOR_DEFAULT;      ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);      if (statusColor != INVALID_VAL)      {        color = statusColor;      }      View statusBarView = new View(activity);      ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,          getStatusBarHeight(activity));      statusBarView.setBackgroundColor(color);      contentView.addView(statusBarView, lp);    }  }  public static void compat(Activity activity)  {    compat(activity, INVALID_VAL);  }  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;  }}

使用方法:

在當(dāng)前activity的onCreate中,調(diào)用方法StatusBarCompat.compat就可以了:

//第二個(gè)參數(shù)是想要設(shè)置的顏色StatusBarCompat.compat(this, Color.RED);

如果嫌每個(gè)activity都要寫(xiě)有點(diǎn)麻煩,那就寫(xiě)個(gè)基類(lèi)來(lái)完成這一步:

public class BaseActivity extends AppCompatActivity {  @Override  protected void onCreate(Bundle savedInstanceState) {    supportRequestWindowFeature(Window.FEATURE_NO_TITLE);    super.onCreate(savedInstanceState);    StatusBarCompat.compat(this, Color.RED);  }}

然后每個(gè)activity的頁(yè)面繼承該BaseActivity就可以了!

關(guān)于上面代碼中提示注意的那個(gè)地方的說(shuō)明:

隱藏系統(tǒng)title注意的兩點(diǎn):

1、繼承AppCompatActivity時(shí)使用:

supportRequestWindowFeature(Window.FEATURENOTITLE)

2、繼承activity時(shí)使用:

requestWindowFeature(Window.FEATURENOTITLE) 

文本相關(guān)下載:點(diǎn)擊免費(fèi)下載源碼及apk文件

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 南投县| 石林| 师宗县| 齐齐哈尔市| 东乡族自治县| 金门县| 亳州市| 迁安市| 玉林市| 墨脱县| 汶上县| 兴文县| 云梦县| 宁国市| 屏边| 博乐市| 夹江县| 鄄城县| 白山市| 贞丰县| 汤原县| 万源市| 长子县| 新邵县| 莫力| 东辽县| 郑州市| 石台县| 杭锦后旗| 武威市| 白银市| 门头沟区| 阿拉善右旗| 瑞昌市| 合江县| 昌平区| 靖州| 乐都县| 辰溪县| 临沧市| 温宿县|