android通知欄高度
㈠ HMA-AL00的電池欄,標題欄通知欄高度
如下
狀態欄是顯示顯示手機狀態(如電池電量、網路狀態、時間、運營商信息等)的區域,一般內容型應用都會顯示保留狀態欄,但是游戲界面如果還保留狀態欄就不合適了,因為游戲界面要響應各種手勢,而狀態欄也會響應一些手勢,有可能引發錯誤操作,所以一般在游戲界面都會隱藏狀態欄,使游戲界面全屏顯示,關於設置全屏請查看《Android設置Activity全屏的兩種方式及Theme屬性解析》,繼續看狀態欄高度測量:
狀態欄高度的測量我在這里提供4種方法:
(1)通過系統尺寸資源獲取
狀態欄高度定義在Android系統尺寸資源中status_bar_height,但這並不是公開可直接使用的,例如像通常使用系統資源那樣android.R.dimen.status_bar_height。但是系統給我們提供了一個Resource類,通過這個類可以獲取資源文件,藉此可以獲取到status_bar_height:
(2)通過R類的反射
大家都知道Android的所有資源都會有惟一標識在R類中作為引用。我們也可以通過反射獲取R類的實例域,然後找status_bar_height:
(3)藉助應用區域的top屬性
這就用到了開題時的那張屏幕區域劃分圖片,狀態欄位於屏幕最頂端,其位置從(0,0)開始,故而應用區域的頂端位置(高度 = Y坐標 - 0)即為狀態欄的高度:
(4)藉助屏幕和應用區域高度
還是看屏幕區域劃分圖,是不是狀態欄占滿了屏幕中除應用區域之外的全部。
㈡ android 自定義通知欄
Notification 參數使用 參考:
http://www.cnblogs.com/kexing/p/8371051.html
自定義通知欄
僅支持FrameLayout、LinearLayout、RelativeLayout三種布局控制項
AnalogClock、Chronometer、Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView和AdapterViewFlipper這些顯示控制項
否則會引起ClassNotFoundException異常。
流程:點擊通知欄 發送廣播 app接收廣播做相應處理:
為通知欄綁定廣播事件:
1.FLAG_CANCEL_CURRENT:如果AlarmManager管理的PendingIntent已經存在,那麼將會取消當前的PendingIntent,從而創建一個新的PendingIntent.
2.FLAG_UPDATE_CURRENT:如果AlarmManager管理的PendingIntent已經存在,讓新的Intent更新之前Intent對象數據,例如更新Intent中的Extras,另外,我們也可以在PendingIntent的原進程中調用PendingIntent的cancel ()把其從系統中移除掉
3.FLAG_NO_CREATE:如果AlarmManager管理的PendingIntent已經存在,那麼將不進行任何操作,直接返回已經.
4.FLAG_ONE_SHOT:該PendingIntent只作用一次.在該PendingIntent對象通過send()方法觸發過後,PendingIntent將自動調用cancel()進行銷毀,那麼如果你再調用send()方法的話,系統將會返回一個SendIntentException.
添加廣播接收:
AndroidManifest:
㈢ android導航欄高度是多少
屏幕高度都是包括了狀態欄和導航欄的高度的
2.獲取控制項尺寸
如果我們在onCreate()方法里直接調用getWidth()、getMeasuredWidth()獲得的尺寸為0,這是由於在onCreate()中,我們的控制項還沒有畫好,等onCreate()執行完了,我們的控制項才被測量出來,我們可以注冊一個監聽器,用來監聽測量結果
ViewTreeObserver vto = mButton.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override
public void onGlobalLayout() { //移除上一次監聽,避免重復監聽
mButton.getViewTreeObserver().removeGlobalOnLayoutListener(this); //在這里調用getHeight()獲得控制項的高度
buttonHeight = mButton.getHeight();
}
});1234567891011
3.獲得狀態欄/通知欄的高度
public static int getStatusBarHeight(Context context){
Class<?> c = null;
Object obj = null;
Field field = null; int x = 0, statusBarHeight = 0; try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
statusBarHeight = context.getResources().getDimensionPixelSize(x);
} catch (Exception e1) {
e1.printStackTrace();
} return statusBarHeight;
}12345678910111213141516
4.獲得導航欄高度
public int getNavigationBarHeight(Activity activity) {
Resources resources = activity.getResources(); int resourceId = resources.getIdentifier("navigation_bar_height","dimen", "android"); //獲取NavigationBar的高度
int height = resources.getDimensionPixelSize(resourceId); return height;
㈣ 如何實現Android沉浸式狀態欄
沉浸式通知欄Android4.4以上才支持的新特性。4.3不支持。
具體實現方式如下:
1.新建個公共style,設置android:fitsSystemWindows=true
<!-- 設置應用布局時是否考慮系統窗口布局;true --> <style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar"> <item name="android:fitsSystemWindows">true</item> </style>
2. 修改AndroidManifest.xml,讓所有的activity樣式默認設置為AppBaseTheme(*不同項目要靈活處理,筆者項目的activity樣式都是統一的所以這樣設置沒問題,但是實際情況下不同的activity可能調用的樣式不一樣,需要讀者自行按自己的項目來設置)
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppBaseTheme" android:name="****">
3.新增沉浸式通知欄實現類,實現原理很簡單。
1)判斷當前系統版本是不是4.4以上,判斷代碼如下:
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT)
2)如果大於4.4則設置狀態欄透明化,代碼如下:
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
3)獲取activity的根rootView(DecorView),然後創建一個新的view stateBarView並把它添加到rootView(這裏手動給它設置個ID,下次進來時先判斷rootView是否已創建stateBarView,如果已創建則直接獲取該View這樣可以防止重復創建,導致內存泄露)
以下是具體代碼實現:
import android.annotation.SuppressLint;import android.app.Activity;import android.content.res.Resources;import android.graphics.drawable.Drawable;import android.os.Build;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.view.WindowManager;import android.widget.FrameLayout.LayoutParams;/** * 沉浸式通知欄公共類 * @author hurrican * */@SuppressLint({ "InlinedApi", "ResourceAsColor" })public class ImmersedNotificationBar { private Activity activity ; //設置沉浸式通知欄的ID(防止重復創建) private final static int IMMERSED_NOTIFICATION_BAR_ID = 12345678 ; private final static String STATUS_BAR_HEIGHT_RES_NAME = "status_bar_height" ; public ImmersedNotificationBar(Activity activity){ this.activity = activity ; } //獲取狀態欄高度 private int getStatusBarHeight(Resources res){ int statusBarHeight = 0; int resourceId = res.getIdentifier(STATUS_BAR_HEIGHT_RES_NAME, "dimen", "android"); if (resourceId > 0) { statusBarHeight = res.getDimensionPixelSize(resourceId); } return statusBarHeight ; } //添加頂部狀態欄 private View addStateBar(Activity activity,ViewGroup rootView,int statusBarHeight){ //創建新的View,並添加到rootView頂部) View statusBarView ; if(null!=rootView.findViewById(IMMERSED_NOTIFICATION_BAR_ID)){ statusBarView = rootView.findViewById(IMMERSED_NOTIFICATION_BAR_ID); }else{ statusBarView = new View(activity); rootView.addView(statusBarView); } statusBarView.setId(IMMERSED_NOTIFICATION_BAR_ID) ; LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,statusBarHeight); params.gravity = Gravity.TOP; statusBarView.setLayoutParams(params); statusBarView.setVisibility(View.VISIBLE); return statusBarView ; } /** * 設置狀態欄顏色 * @param ColorId */ public void setStateBarColor(int ColorId){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = activity.getWindow(); //activity的頂級布局 ViewGroup rootView = (ViewGroup) window.getDecorView(); //透明化狀態欄 window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); Resources res = activity.getResources(); //獲取狀態欄目的高度 int statusBarHeight = getStatusBarHeight(res); View stateBarView = addStateBar(activity,rootView,statusBarHeight) ; stateBarView.setBackgroundColor(ColorId) ; } } /** * 設置狀態欄顏色 * @param ColorId */ public void setStateBarDrawable(Drawable drawable){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = activity.getWindow(); //activity的頂級布局 ViewGroup rootView = (ViewGroup) window.getDecorView(); //透明化狀態欄 window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); Resources res = activity.getResources(); //獲取狀態欄目的高度 int statusBarHeight = getStatusBarHeight(res); View stateBarView = addStateBar(activity,rootView,statusBarHeight) ; stateBarView.setBackgroundDrawable(drawable) ; } }}
㈤ android通知欄高度可以修改么
可以,修改 Android 狀態欄高度:
配置文件:frameworks/base/core/res/res/values/dimens.xml
修改條目:
<resources>
<!-- The width that is used when creating thumbnails of applications. -->
<dimen name="thumbnail_width">0dp</dimen>
<!-- The height that is used when creating thumbnails of applications. -->
<dimen name="thumbnail_height">0dp</dimen>
<!-- The standard size (both width and height) of an application icon that
will be displayed in the app launcher and elsewhere. -->
<dimen name="app_icon_size">48dip</dimen>
<dimen name="toast_y_offset">64dip</dimen>
<!-- Height of the status bar -->
<dimen name="status_bar_height">38dip</dimen>
<!-- Height of the status bar -->
<dimen name="status_bar_icon_size">38dip</dimen>
<!-- Margin at the edge of the screen to ignore touch events for in the windowshade. -->
<dimen name="status_bar_edge_ignore">5dp</dimen>
<!-- Size of the fastscroll hint letter -->
<dimen name="fastscroll_overlay_size">104dp</dimen>
<!-- Width of the fastscroll thumb -->
<dimen name="fastscroll_thumb_width">64dp</dimen>
<!-- Height of the fastscroll thumb -->
<dimen name="fastscroll_thumb_height">52dp</dimen>
<!-- Default height of a key in the password keyboard -->
<dimen name="password_keyboard_key_height">56dip</dimen>
<!-- Default correction for the space key in the password keyboard -->
<dimen name="password_keyboard_spacebar_vertical_correction">4dip</dimen>
</resources>
㈥ android 修改通知欄位置
修改方法:
1、ROOT機器
2、使用ROOT管理器找到root/system/framework/framework-res.apk文件,復制至儲存卡,再復制到電腦上,做好備份
3、用WINRAR打開,進入\res\drawable-mdpi目錄,替換通知欄背景文件以及電池圖標文件。(直接拖入WINRAR,不要解壓)
通知欄背景文件:statusbar_background.9,電池圖標文件:stat_sys_battery_10等
4、將修改後的framework-res.apk文件從SD卡先復制到root/system目錄,修改許可權全部打鉤
5、再從root/system移動到root/system/framework/下
6、重啟手機
可 第3、用WINRAR打開,進入\res\drawable-mdpi目錄,替換通知欄背景文件以及電池圖標文件。(直接拖入WINRAR,不要解壓)