Android應(yīng)用中怎么對(duì)狀態(tài)欄進(jìn)行設(shè)置?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
十年專注成都網(wǎng)站制作,成都企業(yè)網(wǎng)站定制,個(gè)人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識(shí)、方案,網(wǎng)站設(shè)計(jì)流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),專注于成都企業(yè)網(wǎng)站定制,高端網(wǎng)頁制作,對(duì)成都混凝土攪拌站等多個(gè)領(lǐng)域,擁有多年的營(yíng)銷推廣經(jīng)驗(yàn)。
Android 狀態(tài)欄的設(shè)置適配問題詳解
最近看了很多關(guān)于狀態(tài)欄的問題的處理,總結(jié)出處理狀態(tài)欄分兩個(gè)方向1>5.0一下2>5.0以上的手機(jī)狀態(tài)欄的設(shè)置,,,,,,,,這里說的都是自定義的toolbar,我這里已經(jīng)把titlebar給隱藏掉了
(1) 關(guān)于5.0一下:首先我們需要在res文件下的style中設(shè)置,
<!-- Base application theme. --> <style name="AppTheme" parent="AppTheme.Base"> <!-- Customize your theme here. --> </style> <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:windowBackground">@android:color/white</item> <item name="android:windowNoTitle">true</item> </style>
這里我為什么設(shè)置AppTheme.Base 而不直接用AppTheme,因?yàn)槲依^承的是AppCompatActivity,如果直接對(duì)titlebar進(jìn)行隱藏的話不做這個(gè)base處理,程序會(huì)報(bào)錯(cuò),,,,,,如果你繼承的是Activity的話那不會(huì)報(bào)錯(cuò) ,,,,為了處理 版本的更高適配,,,,就必須繼承 AppCompatActivity ,,,所以在適配5.0一下的系統(tǒng)的時(shí)候,就要做一個(gè) base 來過渡 加載 style...
5.0一下的到此就結(jié)束了....
(2) 關(guān)于5.0以上的版本,,我門要在res下方增加一個(gè) values-21文件,結(jié)構(gòu)如圖
置于style中的代碼:如下
<style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="android:windowDrawsSystemBarBackgrounds">true</item> <item name="android:statusBarColor">@android:color/transparent</item> </style> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:windowDrawsSystemBarBackgrounds">true</item> <!-- 標(biāo)題顏色 --> <item name="android:textColorPrimary">#000</item> <!-- 溢出菜單圖標(biāo)顏色 --> <item name="colorControlNormal">@color/white</item> <!-- 箭頭 --> <item name="drawerArrowStyle">@style/DrawerArrowStyle</item> <!-- 溢出菜單文字顏色 --> <item name="textAppearanceLargePopupMenu">@style/OverflowMenuTextAppearance</item> <!-- 菜單項(xiàng)點(diǎn)擊selector --> <item name="actionBarItemBackground">@drawable/abc_item_background_holo_dark</item> </style> <!-- 左邊的箭頭指示 --> <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle"> <item name="spinBars">true</item> <item name="color">@color/white</item> </style> <!-- 溢出菜單文字樣式 --> <style name="OverflowMenuTextAppearance" parent="@style/TextAppearance.AppCompat.Widget.PopupMenu.Large"> <item name="android:textColor">@color/white</item> </style>
手機(jī)會(huì) 根據(jù) 你手機(jī)的系統(tǒng)版本去加載,這兩個(gè)style,5.x以上的 加載第二個(gè) ,針對(duì) 不同的系統(tǒng)版本修改對(duì)應(yīng)的style..........
當(dāng)然 這兩步足夠做適配的了,但是 我們 可能需要進(jìn)入不同界面 顯示不通的狀態(tài)欄,,,這時(shí)候 就要我們用代碼控制,修改了,,,,代碼 我也直接貼出來 ,,,一下 代碼 是 看別人的.......
(1) 在activity設(shè)置 這些,,,
int color = getResources().getColor(R.color.red); // View view = LayoutInflater.from(this).inflate(R.layout.statusbar,null); StatusBarManager statusBarManager = new StatusBarManager(this,color); //這里如果要移動(dòng)titlebar,則在布局中指定為include的自定義statusbar的view //如果不指定,則調(diào)用setStatusBarView();會(huì)自動(dòng)加一個(gè)view // statusBarManager.setStatusBarView(view); statusBarManager.setStatusBarView();
(2)重寫 一個(gè)類StatusBarManager 進(jìn)行修改 :
package com.example.administrator.statusbartest; import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Build; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.FrameLayout; import android.widget.LinearLayout; import java.lang.reflect.Method; /** * Created by Administrator on 2017/3/21. */ public class StatusBarManager { private static final int BUILD_VERSION_KITKAT = 19; private static final int BUILD_VERSION_LOLLIPOP = 21; //WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS public static final int FLAG_TRANSLUCENT_STATUS = 0x04000000; //WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS public static final int FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS = 0x80000000; private Activity mActivity; private View statusBarView; private int statusBarHeight; //設(shè)置狀態(tài)藍(lán)的顏色值 int color; public StatusBarManager(Activity activity,int color) { this.mActivity = activity; this.color = color; statusBarHeight = getStatusBarHeight(activity); } public void setStatusBarView(View statusBarView) { this.statusBarView = statusBarView; setTransparent(); } public void setStatusBarView() { setTransparent(); } public int getStatusBarHeight() { return statusBarHeight; } /** * 設(shè)置狀態(tài)欄全透明 * */ private void setTransparent() { //4.0沒有轉(zhuǎn)太爛 if (Build.VERSION.SDK_INT < BUILD_VERSION_KITKAT) { return; } if(statusBarHeight <= 0){ return; } transparentStatusBar(); showStatusBarView(); } @TargetApi(19) private void showStatusBarView() { /** * 設(shè)置狀態(tài)欄顏色的位置 */ if(statusBarView == null){ statusBarView = new View(mActivity); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(mActivity)); statusBarView.setLayoutParams(params); statusBarView.setBackgroundColor(color); ViewGroup decorView = (ViewGroup) mActivity.getWindow().getDecorView(); FrameLayout content = (FrameLayout) decorView.findViewById(android.R.id.content); FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) content.getChildAt(0).getLayoutParams(); layoutParams.setMargins(0,statusBarHeight,0,0); decorView.addView(statusBarView); }else{ ViewGroup.LayoutParams layoutParams = statusBarView.getLayoutParams(); layoutParams.height = getStatusBarHeight(mActivity); statusBarView.setLayoutParams(layoutParams); statusBarView.setBackgroundColor(color); } } /** * 參考上面注釋掉的代碼 因?yàn)樾枰秒[藏API 調(diào)用方式進(jìn)行改成反射 */ private void transparentStatusBar(){ Window window = mActivity.getWindow(); if (Build.VERSION.SDK_INT >= BUILD_VERSION_LOLLIPOP) { //不add此條flag 會(huì)導(dǎo)致在EMUI3.1(華為)上失效,add這個(gè)flag 會(huì)導(dǎo)致在其它機(jī)型上面添加一個(gè)半透明黑條 window.addFlags(FLAG_TRANSLUCENT_STATUS); //下面的代碼段是不加上面的flag時(shí),要顯示純色的狀態(tài)欄時(shí)需要加的代碼 不用了 /* window.clearFlags(FLAG_TRANSLUCENT_STATUS); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); window.addFlags(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);*/ //因?yàn)樾枰秒[藏API,沒有重新編譯5.x版本的android.jar,使用的還是18的api,這里用的反射 try { Class[] argsClass=new Class[]{int.class}; Method setStatusBarColorMethod = Window.class.getMethod("setStatusBarColor",argsClass); setStatusBarColorMethod.invoke(window, Color.TRANSPARENT); } catch (Exception e) { e.printStackTrace(); } }else{ window.addFlags(FLAG_TRANSLUCENT_STATUS); } } /** * 獲取狀態(tài)欄高度 * * @param context context * @return 狀態(tài)欄高度 */ private static int getStatusBarHeight(Context context) { if (Build.VERSION.SDK_INT < BUILD_VERSION_KITKAT) { return 0; } // 獲得狀態(tài)欄高度 int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); return context.getResources().getDimensionPixelSize(resourceId); } public void setStatusbarVisibility(int visibility){ if(statusBarView != null) { this.statusBarView.setVisibility(visibility); } } public void setColor(int color){ if(statusBarView != null){ this.statusBarView.setBackgroundColor(color); } } }
看完上述內(nèi)容,你們掌握Android應(yīng)用中怎么對(duì)狀態(tài)欄進(jìn)行設(shè)置的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
當(dāng)前文章:Android應(yīng)用中怎么對(duì)狀態(tài)欄進(jìn)行設(shè)置
標(biāo)題來源:http://jinyejixie.com/article48/johehp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站策劃、靜態(tài)網(wǎng)站、微信公眾號(hào)、網(wǎng)站制作、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)