国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【Android】獲取導航欄、狀態(tài)欄高度

這篇具有很好參考價值的文章主要介紹了【Android】獲取導航欄、狀態(tài)欄高度。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

獲取狀態(tài)欄高度

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;
}

或者

getWindow().getDecorView().setOnApplyWindowInsetsListener((v, insets) -> {
    int statuBarHeight = insets.getStableInsetTop();
    return insets;
});

獲取導航欄高度

public static int getNavigationBarHeight(Context context) {
    int result = 0;
    int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = context.getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

或者

getWindow().getDecorView().setOnApplyWindowInsetsListener((v, insets) -> {
    int navigationBarHeight = insets.getStableInsetBottom();
    return insets;
});

其他數(shù)據(jù)

系統(tǒng)的各種數(shù)據(jù)定義位于SDK的xml文件中:

android-30/data/res/values/dimens.xml

<dimen name="toast_y_offset">24dp</dimen>
<!-- Height of the status bar -->
<dimen name="status_bar_height">@dimen/status_bar_height_portrait</dimen>
<!-- Height of the status bar in portrait. The height should be
     Max((status bar content height + waterfall top size), top cutout size) -->
<dimen name="status_bar_height_portrait">24dp</dimen>
<!-- Height of the status bar in landscape. The height should be
     Max((status bar content height + waterfall top size), top cutout size) -->
<dimen name="status_bar_height_landscape">@dimen/status_bar_height_portrait</dimen>
<!-- Height of the bottom navigation / system bar. -->
<dimen name="navigation_bar_height">48dp</dimen>
<!-- Height of the bottom navigation bar in portrait; often the same as @dimen/navigation_bar_height -->
<dimen name="navigation_bar_height_landscape">48dp</dimen>
<!-- Width of the navigation bar when it is placed vertically on the screen -->
<dimen name="navigation_bar_width">48dp</dimen>

通過key可以獲取對應的值。

修改導航欄、狀態(tài)欄背景色

getWindow().setStatusBarColor(Color.RED);
getWindow().setNavigationBarColor(Color.RED);

源碼

導航欄和狀態(tài)欄源碼相似

#PhoneWindow
@Override
public void setStatusBarColor(int color) {
    mStatusBarColor = color;
    mForcedStatusBarColor = true;
    if (mDecor != null) {
        mDecor.updateColorViews(null, false /* animate */);
    }
}

#DecorView

public static final ColorViewAttributes STATUS_BAR_COLOR_VIEW_ATTRIBUTES =
        new ColorViewAttributes(SYSTEM_UI_FLAG_FULLSCREEN, FLAG_TRANSLUCENT_STATUS,
                Gravity.TOP, Gravity.LEFT, Gravity.RIGHT,
                Window.STATUS_BAR_BACKGROUND_TRANSITION_NAME,
                com.android.internal.R.id.statusBarBackground,
                FLAG_FULLSCREEN, ITYPE_STATUS_BAR);

public static final ColorViewAttributes NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES =
        new ColorViewAttributes(
                SYSTEM_UI_FLAG_HIDE_NAVIGATION, FLAG_TRANSLUCENT_NAVIGATION,
                Gravity.BOTTOM, Gravity.RIGHT, Gravity.LEFT,
                Window.NAVIGATION_BAR_BACKGROUND_TRANSITION_NAME,
                com.android.internal.R.id.navigationBarBackground,
                0 , ITYPE_NAVIGATION_BAR);


private final ColorViewState mStatusColorViewState = new ColorViewState(STATUS_BAR_COLOR_VIEW_ATTRIBUTES);
private final ColorViewState mNavigationColorViewState = new ColorViewState(NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES);

WindowInsets updateColorViews(WindowInsets insets, boolean animate) {
	
	...
	
    updateColorViewInt(mNavigationColorViewState, sysUiVisibility,
             calculateNavigationBarColor(), mWindow.mNavigationBarDividerColor, navBarSize,
             navBarToRightEdge || navBarToLeftEdge, navBarToLeftEdge,
             0 /* sideInset */, animate && !disallowAnimate,
             mForceWindowDrawsBarBackgrounds, controller);
	
	...
	
    updateColorViewInt(mStatusColorViewState, sysUiVisibility,
              calculateStatusBarColor(), 0, mLastTopInset,
              false /* matchVertical */, statusBarNeedsLeftInset, statusBarSideInset,
              animate && !disallowAnimate,
              mForceWindowDrawsBarBackgrounds, controller);

}

調(diào)用updateColorViews更新背景色的方法,還有如下位置:
android 獲取狀態(tài)欄高度,android,android

#ViewRootImpl
private void performTraversals() {
	
	dispatchApplyInsets(host);

}

public void dispatchApplyInsets(View host) {
     mApplyInsetsRequested = false;
     WindowInsets insets = getWindowInsets(true /* forceConstruct */);
     host.dispatchApplyWindowInsets(insets);
	...
}

#View

public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) {
    ...
    return onApplyWindowInsets(insets);
	...
}

#DecorView

@Override
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
    ...
    insets = updateColorViews(insets, true);
    ...
    return insets;
}

關于WindowInsets

WindowInsets可以翻譯為窗口附加物,一般是指一個界面中,不由開發(fā)者直接控制的部分,例如:狀態(tài)欄、底部的導航欄等等。具體的類型在Type中有定義:

#WindowInsets.Type
static final int FIRST = 1 << 0;
static final int STATUS_BARS = FIRST;
static final int NAVIGATION_BARS = 1 << 1;
static final int CAPTION_BAR = 1 << 2;

static final int IME = 1 << 3;

static final int SYSTEM_GESTURES = 1 << 4;
static final int MANDATORY_SYSTEM_GESTURES = 1 << 5;
static final int TAPPABLE_ELEMENT = 1 << 6;

static final int DISPLAY_CUTOUT = 1 << 7;

static final int LAST = 1 << 8;
static final int SIZE = 9;
static final int WINDOW_DECOR = LAST;

可以向DecorView添加監(jiān)聽,處理WindowInsets

getWindow().getDecorView().setOnApplyWindowInsetsListener((v, insets) -> {
    return insets;
});

所以,可以在這里獲取導航欄和狀態(tài)欄的高度:

getWindow().getDecorView().setOnApplyWindowInsetsListener((v, insets) -> {
    int statuBarHeight = insets.getStableInsetTop();
    int navigationBarHeight = insets.getStableInsetBottom();
    return insets;
});

也可以修改導航欄和狀態(tài)欄的高度(好像沒有應用場景):文章來源地址http://www.zghlxwxcb.cn/news/detail-520367.html

getWindow().getDecorView().setOnApplyWindowInsetsListener((v, insets) -> {
    int statuBarHeight = 0;
    int navigationBarHeight = 0;
    return insets.replaceSystemWindowInsets(0,statuBarHeight,0,navigationBarHeight);
;
});

到了這里,關于【Android】獲取導航欄、狀態(tài)欄高度的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 微信小程序如何實現(xiàn)自定義導航欄、導航欄手機適配(獲取導航欄、狀態(tài)欄高度和膠囊位置)以及踩過的坑

    微信小程序如何實現(xiàn)自定義導航欄、導航欄手機適配(獲取導航欄、狀態(tài)欄高度和膠囊位置)以及踩過的坑

    背景:不用官方默認的導航欄,想用自己自定義的 實現(xiàn)效果: :頂部狀態(tài)欄、頂部導航欄、頂部狀態(tài)導航欄、膠囊 原理: 自定義導航欄無非就是求得導航欄高度,并讓內(nèi)容容器垂直方向居中于導航欄高度 1.獲取手機系統(tǒng)狀態(tài)欄高度 2.獲取膠囊位置(包括高度) 3.求得

    2024年03月12日
    瀏覽(32)
  • Android獲取文本的寬度和高度

    Android獲取文本的寬度和高度

    方法一:先繪制文本所在的矩形區(qū)域,再獲取矩形區(qū)域的寬度 上述方法由于矩形邊框緊貼文字,所有沒有多余的空間。 方法二:通過Paint的 measureText 方法直接測量文本寬度 此方法計算出的寬度會加上開始和結尾的空間,這個空間就是文字和文字之間的空間,為了美觀而存在

    2024年02月09日
    瀏覽(21)
  • Android WindowInsetsController 設置狀態(tài)欄、導航欄

    Android WindowInsetsController 設置狀態(tài)欄、導航欄

    有 hide() ,也有 show() 在更早的版本中,使用 ViewCompat.getWindowInsetsController() 獲取 WindowInsetsControllerCompat 實例 而現(xiàn)在推薦使用 WindowCompat.etInsetsController() 獲取 WindowInsetsControllerCompat 實例 底部的三個按鍵就是導航欄 (navigation bar): back / home / recent 。 高版本系統(tǒng),recent,可能沒有圖

    2024年02月15日
    瀏覽(32)
  • android 12.0狀態(tài)欄高度為0時,系統(tǒng)全局手勢失效的解決方案

    在12.0的framework 系統(tǒng)全局手勢事件也是系統(tǒng)非常重要的功能,但是當隱藏狀態(tài)欄, 當把狀態(tài)欄高度設置為0時,這時全局手勢事件失效,這就要從系統(tǒng)手勢滑動流程來分析 看怎么樣實現(xiàn)系統(tǒng)手勢功能的,然后根據(jù)功能做修改

    2024年02月07日
    瀏覽(24)
  • Android 顯示、隱藏狀態(tài)欄和導航欄

    控制狀態(tài)欄顯示,Activity的主題中配置全屏屬性 控制狀態(tài)欄顯示,在setContentView之前設置全屏的flag 復制代碼 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 控制狀態(tài)欄顯示,在任何位置通過添加和移除全屏的flag 控制狀態(tài)欄和導航欄顯示,

    2024年02月08日
    瀏覽(26)
  • Android隱藏導航欄和狀態(tài)欄的方法

    一。去除狀態(tài)欄 以下是Android去除狀態(tài)欄的代碼示例: 1. 在Activity的onCreate()方法中添加以下代碼: getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 2. 在AndroidManifest.xml文件中的Activity節(jié)點中添加以下屬性: android:theme=\\\"@android:style/Theme.NoTitleB

    2024年02月16日
    瀏覽(43)
  • 史上最完美的Android沉浸式狀態(tài)導航欄攻略

    史上最完美的Android沉浸式狀態(tài)導航欄攻略

    最近我在小破站開發(fā)一款新App,叫 高能鏈 。我是一個完美主義者,所以不管對架構還是UI,我都是比較摳細節(jié)的,在狀態(tài)欄和導航欄沉浸式這一塊,我還是踩了挺多坑,費了挺多精力的。這次我將我踩坑,適配各機型總結出來的史上最完美的Android沉浸式狀態(tài)導航欄攻略分享

    2023年04月26日
    瀏覽(18)
  • uniapp 小程序自定義導航欄計算狀態(tài)欄(頂部)與導航欄(膠囊)高度

    uniapp 小程序自定義導航欄計算狀態(tài)欄(頂部)與導航欄(膠囊)高度

    uni.getMenuButtonBoundingClientRect() 參考鏈接 uni.getSystemInfo()參考鏈接

    2024年02月11日
    瀏覽(26)
  • Android 標題欄、狀態(tài)欄、系統(tǒng)欄、導航欄、應用欄及各個位置的顏色設置

    Android 標題欄、狀態(tài)欄、系統(tǒng)欄、導航欄、應用欄及各個位置的顏色設置

    如上圖,可以看到,有狀態(tài)欄(status bar)、標題欄(action bar, toolbar)、導航欄(navigation bar) 等, 狀態(tài)欄 (status bar):是指手機最頂上,顯示中國移動、安全衛(wèi)士、電量、網(wǎng)速等等,在手機的頂部。下拉就會出現(xiàn)通知欄。 標題欄就是指action bar/toolbar,app程序最上邊的titlebar。關于acti

    2023年04月16日
    瀏覽(36)
  • Android全屏彈出Dialog顯示狀態(tài)欄和導航欄的問題及解決方案

    在移動端開發(fā)中,有時候我們需要在Android應用中彈出一個全屏的Dialog。然而,當我們嘗試實現(xiàn)這樣的Dialog時,可能會遇到一個問題:狀態(tài)欄和導航欄在全屏Dialog中仍然可見,這可能會影響用戶體驗。本文將介紹如何解決這個問題,并提供相應的源代碼。 問題描述: 當我們使

    2024年02月05日
    瀏覽(30)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包