這篇文章將為大家詳細(xì)講解有關(guān)如何實(shí)現(xiàn)仿ios狀態(tài)欄顏色和標(biāo)題欄顏色一致,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)是一家從事企業(yè)網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、行業(yè)門戶網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)制作的專業(yè)網(wǎng)站建設(shè)公司,擁有經(jīng)驗(yàn)豐富的網(wǎng)站建設(shè)工程師和網(wǎng)頁設(shè)計(jì)人員,具備各種規(guī)模與類型網(wǎng)站建設(shè)的實(shí)力,在網(wǎng)站建設(shè)領(lǐng)域樹立了自己獨(dú)特的設(shè)計(jì)風(fēng)格。自公司成立以來曾獨(dú)立設(shè)計(jì)制作的站點(diǎn)上千多家。
首先創(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); } } }
關(guān)于“如何實(shí)現(xiàn)仿ios狀態(tài)欄顏色和標(biāo)題欄顏色一致”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。