您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)如何實(shí)現(xiàn)仿ios狀態(tài)欄顏色和標(biāo)題欄顏色一致,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
首先創(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)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。