Android 高仿QQ 沉浸式狀態(tài)欄
前言:
在進(jìn)入今天正題前,還是老樣子先談?wù)劯邢氚桑罱杏X(jué)整個(gè)都失去了方向感,好迷茫!找工作又失敗了,難道Android真的飽和了?這兩天我一直沒(méi)出門(mén),除了下樓哪外賣(mài)就是宅宿舍了,靜想了許久,我還是不能忘了初心,我相信我找不到工作的原因有很多,最關(guān)鍵的還是要技術(shù)夠硬才行啊,奔跑吧孩子!接下來(lái)我就給大家介紹怎樣快速打造沉浸式狀態(tài)欄吧,雖然感覺(jué)有點(diǎn)相見(jiàn)恨晚,但其實(shí)不完!
一:何為沉浸式狀態(tài)欄?
沉浸式狀態(tài)欄是Google從Android 4.4開(kāi)始,給我們開(kāi)發(fā)者提供的一套能透明的系統(tǒng)ui樣式,這樣樣式是給狀態(tài)欄和導(dǎo)航欄的,這樣的話就不用向以前那樣每天面對(duì)著黑乎乎的上下兩條黑欄了,還可以調(diào)成跟Activity一樣的樣式,形成一個(gè)完整的主題,和IOS7.0以上系統(tǒng)一樣了。
先給你們來(lái)對(duì)比一下加了沉浸式和沒(méi)加沉浸式的樣式效果圖吧,如下圖所示:
(非沉浸式)
(沉浸式)
在此相信大家都了解什么是沉浸式狀態(tài)欄了;目前打開(kāi)很多APP都會(huì)有這種效果,可想而知,沉浸式狀態(tài)欄還是挺實(shí)用的!
二、使用沉浸式狀態(tài)欄高仿QQ:
實(shí)現(xiàn)沉浸式狀態(tài)欄的步驟灰常簡(jiǎn)單:因?yàn)槌两綘顟B(tài)欄是Android4.4后才推出的,所以首先在程序中加上判斷,即:當(dāng)系統(tǒng)版本為4.4或者4.4以上時(shí)可以使用沉浸式狀態(tài)欄,接著在語(yǔ)句中設(shè)置狀態(tài)欄和導(dǎo)航欄為透明即可:
//當(dāng)系統(tǒng)版本為4.4或者4.4以上時(shí)可以使用沉浸式狀態(tài)欄 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態(tài)欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導(dǎo)航欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); }
最后在布局文件中加上、、、
android:fitsSystemWindows="true" android:clipToPadding="true"
這樣就實(shí)現(xiàn)沉浸式狀態(tài)欄了!
就是這么簡(jiǎn)單,就是這么耐使!源碼是最好的導(dǎo)師,快看看整體代碼吧:
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" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical" tools:context=".MainActivity"> <RelativeLayout android:fitsSystemWindows="true" android:clipToPadding="true" android:layout_width="match_parent" android:layout_height="80dp" android:background="#0099cc"> <de.hdodenhof.circleimageview.CircleImageView android:id="@+id/img_head" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="center_horizontal" android:layout_marginTop="13dp" android:layout_marginLeft="15dp" app:civ_border_width="2dp" app:civ_border_color="#FFFFFF" android:src="@mipmap/meinv"> </de.hdodenhof.circleimageview.CircleImageView> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="26dp" android:layout_centerHorizontal="true" android:text="聯(lián)系人" android:textColor="@android:color/white" android:textSize="18sp" /> <TextView android:id="@+id/tv_right_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="26dp" android:layout_marginRight="15dp" android:layout_alignParentRight="true" android:text="添加" android:textColor="@android:color/white" android:textSize="18sp" /> </RelativeLayout> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="沉浸式狀態(tài)欄" android:textSize="22sp" android:background="#E0FFFF"/> </LinearLayout>
MainActivity中:
package com.zsml.chaotranstintbar; import android.app.Activity; import android.os.Build; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); //當(dāng)系統(tǒng)版本為4.4或者4.4以上時(shí)可以使用沉浸式狀態(tài)欄 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態(tài)欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導(dǎo)航欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } } }
實(shí)現(xiàn)沉浸式狀態(tài)欄的其他方法:動(dòng)態(tài)加入、第三方庫(kù)。
1、動(dòng)態(tài)實(shí)現(xiàn):
動(dòng)態(tài)實(shí)現(xiàn)也是比較簡(jiǎn)單的,首先是隱藏布局,最后動(dòng)態(tài)計(jì)算狀態(tài)欄高度并設(shè)置,都是在MainActivity中操作的,布局文件也就不用加上 Android:fitsSystemWindows="true"、 android:clipToPadding="true" 這兩句了!
所以直接給源碼吧:
MainActivity中:
import android.app.Activity; import android.os.Build; import android.os.Bundle; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.LinearLayout; import java.lang.reflect.Field; public class TwoActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //去掉標(biāo)題 this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_two); //當(dāng)系統(tǒng)版本為4.4或者4.4以上時(shí)可以使用沉浸式狀態(tài)欄 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態(tài)欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導(dǎo)航欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); LinearLayout linear_bar=(LinearLayout)findViewById(R.id.linear_bar); linear_bar.setVisibility(View.VISIBLE); int statusHeight=getStatusBarHeight(); LinearLayout.LayoutParams params=(LinearLayout.LayoutParams )linear_bar.getLayoutParams(); params.height=statusHeight; linear_bar.setLayoutParams(params); } } /** * 獲取狀態(tài)欄的高度 * @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; } }
這樣就完事了,是不是一樣那么簡(jiǎn)單、、、
2、第三方庫(kù)實(shí)現(xiàn)(SystemBarTint):
SystemBarTint是開(kāi)源到github上的一個(gè)開(kāi)源庫(kù)來(lái)的;
地址:https://github.com/jgilfelt/SystemBarTint
使用步驟:
關(guān)聯(lián)庫(kù):compile'com.readystatesoftware.systembartint:systembartint:1.0.3'
xml布局中添加:
android:fitsSystemWindows="true" android:clipToPadding="true" MainActivity中實(shí)現(xiàn): import android.app.Activity; import android.os.Build; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; import com.readystatesoftware.systembartint.SystemBarTintManager; public class ThreeActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_three); //當(dāng)系統(tǒng)版本為4.4或者4.4以上時(shí)可以使用沉浸式狀態(tài)欄 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態(tài)欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導(dǎo)航欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); SystemBarTintManager tintManager = new SystemBarTintManager(this); // 激活狀態(tài)欄 tintManager.setStatusBarTintEnabled(true); // enable navigation bar tint 激活導(dǎo)航欄 tintManager.setNavigationBarTintEnabled(true); //設(shè)置系統(tǒng)欄設(shè)置顏色 //tintManager.setTintColor(R.color.red); //給狀態(tài)欄設(shè)置顏色 tintManager.setStatusBarTintResource(R.color.middle_color); // 設(shè)置導(dǎo)航欄設(shè)置資源 tintManager.setNavigationBarTintResource(R.color.androidColorE); } } }
都是大同小異來(lái)的,我個(gè)人覺(jué)得第一種方法是最好實(shí)現(xiàn)和理解的,大家都可以嘗試一下,希望對(duì)你們有所幫助!最后貼出沉浸式狀態(tài)欄-高仿QQ的效果圖如下:
以上所述是小編給大家介紹的Android 高仿QQ 沉浸式狀態(tài)欄,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android 實(shí)現(xiàn)沉浸式狀態(tài)欄的方法
- Android 沉浸式狀態(tài)欄與隱藏導(dǎo)航欄實(shí)例詳解
- 解決Android 沉浸式狀態(tài)欄和華為虛擬按鍵沖突問(wèn)題
- Android之沉浸式狀態(tài)欄的實(shí)現(xiàn)方法、狀態(tài)欄透明
- Android沉浸式狀態(tài)欄微技巧(帶你真正理解沉浸式模式)
- Android 4.4以上"沉浸式"狀態(tài)欄效果的實(shí)現(xiàn)方法
- Android App仿QQ制作Material Design風(fēng)格沉浸式狀態(tài)欄
- Android編程中沉浸式狀態(tài)欄的三種實(shí)現(xiàn)方式詳解
- 另外兩種Android沉浸式狀態(tài)欄實(shí)現(xiàn)思路
- Android實(shí)現(xiàn)沉浸式狀態(tài)欄功能
相關(guān)文章
Flutter實(shí)現(xiàn)文本滾動(dòng)高亮效果的示例講解
這篇文章主要介紹了如何利用Flutter時(shí)時(shí)渲染頁(yè)面從而達(dá)到文本滾動(dòng)高亮的效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-03-03Android使用TextView實(shí)現(xiàn)無(wú)下劃線超鏈接的方法
這篇文章主要介紹了Android使用TextView實(shí)現(xiàn)無(wú)下劃線超鏈接的方法,結(jié)合實(shí)例形式分析了Android中TextView超鏈接去除下劃線的相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2016-08-08React Native 實(shí)現(xiàn)熱更新并自動(dòng)簽名打包功能
這篇文章主要介紹了React Native 實(shí)現(xiàn)熱更新并自動(dòng)簽名打包,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Android中解決EditText放到popupWindow中,原有復(fù)制、粘貼、全選、選擇功能失效問(wèn)題
這篇文章主要介紹了Android中解決EditText放到popupWindow中,原有復(fù)制、粘貼、全選、選擇功能失效問(wèn)題 的相關(guān)資料,需要的朋友可以參考下2016-04-04Android開(kāi)發(fā)之子線程操作UI的幾種方法
這篇文章主要介紹了Android開(kāi)發(fā)之子線程操作UI的幾種方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08Android中findViewById返回為空null的快速解決辦法
這篇文章主要介紹了Android中findViewById返回為空null的快速解決辦法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06Android Intent與IntentFilter案例詳解
這篇文章主要介紹了Android Intent與IntentFilter案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Android添加ButterKnife時(shí)報(bào)錯(cuò)Error:(2, 0) Cannot add extension wit
今天小編就為大家分享一篇關(guān)于Android添加ButterKnife時(shí)報(bào)錯(cuò)Error:(2, 0) Cannot add extension with name 'android'的解決辦法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12Android RecyclerView的簡(jiǎn)單使用
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView簡(jiǎn)單使用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03