仿ios狀態(tài)欄顏色和標(biāo)題欄顏色一致的實(shí)例代碼
首先創(chuàng)建一個(gè)工具類
import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; import android.os.Build; import android.support.v4.content.ContextCompat; import android.view.View; import android.view.ViewGroup; public class StatusBarCompat { private static final int INVALID_VAL = -1; //判斷版本 @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static View compat(Activity activity, int statusColor) { //默認(rèn)顏色 int color = ContextCompat.getColor(activity, R.color.colorPrimaryDark); //若果版本大于等于21 給狀態(tài)欄設(shè)置顏色 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (statusColor != INVALID_VAL) { color = statusColor; } activity.getWindow().setStatusBarColor(color); return null; } //如果版本在大于等于19小于21 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { //獲取contentView ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content); if (statusColor != INVALID_VAL) { color = statusColor; } //獲取contentView里的第一個(gè)view 也就是狀態(tài)欄 View statusBarView = contentView.getChildAt(0); if (statusBarView != null && statusBarView.getMeasuredHeight() == getStatusBarHeight(activity)) { statusBarView.setBackgroundColor(color); return statusBarView; } statusBarView = new View(activity); ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity)); statusBarView.setBackgroundColor(color); contentView.addView(statusBarView, lp); return statusBarView; } return null; } 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; } }
然后在activity里調(diào)用方法
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v4.content.ContextCompat; import android.os.Build; import android.view.WindowManager; public class MainActivity extends AppCompatActivity { protected boolean statusBarCompat = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (statusBarCompat) { StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.colorPrimary)); transparent19and20(); } } protected void transparent19and20() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } } }
抄的別人的 網(wǎng)上一大堆,作者不要來噴我,我是為了自己以后好找到使用。
以上這篇仿ios狀態(tài)欄顏色和標(biāo)題欄顏色一致的實(shí)例代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
動(dòng)態(tài)添加LinearLayout的高度實(shí)例
下面小編就為大家?guī)硪黄獎(jiǎng)討B(tài)添加LinearLayout的高度實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04BootStrapValidator與My97日期校驗(yàn)的實(shí)例代碼
這篇文章給大家介紹了bootstrapvalidator與my97日期校驗(yàn)的實(shí)例代碼,代碼簡單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-01-01Android自定義View實(shí)現(xiàn)天氣預(yù)報(bào)折線圖
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)天氣預(yù)報(bào)折線圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Android定時(shí)器和Handler用法實(shí)例分析
這篇文章主要介紹了Android定時(shí)器和Handler用法,實(shí)例分析了Android中的定時(shí)器與Handler相關(guān)使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04Android音頻系統(tǒng)AudioTrack使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android音頻系統(tǒng)AudioTrack的使用方法,如何使用AudioTrack進(jìn)行音頻播放,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android控件系列之相冊Gallery&Adapter適配器入門&控件縮放動(dòng)畫入門
本文介紹了如何使用Gallery打造簡單的相冊,并實(shí)現(xiàn)了與用戶點(diǎn)擊的互動(dòng)動(dòng)畫,并介紹了適配器的原理。您可以在此基礎(chǔ)上修改,實(shí)現(xiàn)自己的相冊,嵌入到任何程序中都會增色不少2012-11-11使用genymotion訪問本地上Tomcat上數(shù)據(jù)的方法
下面小編就為大家?guī)硪黄褂胓enymotion訪問本地上Tomcat上數(shù)據(jù)的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03Android SDK Manager國內(nèi)無法更新的解決方案
本文主要介紹Android SDK Manager國內(nèi)無法更新的解決方案,這里提供了解決方法,及簡單說明實(shí)現(xiàn)流程,有興趣的小伙伴可以參考下2016-09-09