תגובה לשינויים בהרשאות הגישה בממשק המשתמש
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
בשיעור הזה נסביר איך לרשום מאזינים כדי שהאפליקציה תוכל לקבל התראה
שינויים בהרשאות הגישה לממשק המשתמש של המערכת. זה שימושי אם רוצים
לסנכרן חלקים אחרים של ממשק המשתמש עם ההסתרה/הצגה של סרגלי המערכת.
רישום האזנה
כדי לקבל התראה על שינויים בהרשאות הגישה לממשק המשתמש של המערכת, צריך לרשום
View.OnSystemUiVisibilityChangeListener
לתצוגה שלך.
בדרך כלל זו התצוגה שמשמשת אתכם כדי לשלוט בחשיפה של הניווט.
לדוגמה, אפשר להוסיף את הקוד הזה לפעילות
אמצעי תשלום אחד (onCreate()
):
Kotlin
window.decorView.setOnSystemUiVisibilityChangeListener { visibility ->
// Note that system bars will only be "visible" if none of the
// LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
// TODO: The system bars are visible. Make any desired
// adjustments to your UI, such as showing the action bar or
// other navigational controls.
} else {
// TODO: The system bars are NOT visible. Make any desired
// adjustments to your UI, such as hiding the action bar or
// other navigational controls.
}
}
Java
View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
// Note that system bars will only be "visible" if none of the
// LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
// TODO: The system bars are visible. Make any desired
// adjustments to your UI, such as showing the action bar or
// other navigational controls.
} else {
// TODO: The system bars are NOT visible. Make any desired
// adjustments to your UI, such as hiding the action bar or
// other navigational controls.
}
}
});
בדרך כלל מומלץ לשמור על סנכרון בין ממשק המשתמש לשינויים בסרגל המערכת
החשיפה. לדוגמה, ניתן להשתמש ב-listener הזה כדי להסתיר ולהציג את סרגל הפעולות
עם הסתרה והצגה של שורת הסטטוס.
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. Java ו-OpenJDK הם סימנים מסחריים או סימנים מסחריים רשומים של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-07-27 (שעון UTC).
[[["התוכן קל להבנה","easyToUnderstand","thumb-up"],["התוכן עזר לי לפתור בעיה","solvedMyProblem","thumb-up"],["סיבה אחרת","otherUp","thumb-up"]],[["חסרים לי מידע או פרטים","missingTheInformationINeed","thumb-down"],["התוכן מורכב מדי או עם יותר מדי שלבים","tooComplicatedTooManySteps","thumb-down"],["התוכן לא עדכני","outOfDate","thumb-down"],["בעיה בתרגום","translationIssue","thumb-down"],["בעיה בדוגמאות/בקוד","samplesCodeIssue","thumb-down"],["סיבה אחרת","otherDown","thumb-down"]],["עדכון אחרון: 2025-07-27 (שעון UTC)."],[],[],null,["# Respond to UI visibility changes\n\nThis lesson describes how to register a listener so that your app can get notified\nof system UI visibility changes. This is useful if you want to\nsynchronize other parts of your UI with the hiding/showing of system bars.\n\nRegister a Listener\n-------------------\n\nTo get notified of system UI visibility changes, register an\n[View.OnSystemUiVisibilityChangeListener](/reference/android/view/View.OnSystemUiVisibilityChangeListener) to your view.\nThis is typically the view you are using to control the navigation visibility.\n\nFor example, you could add this code to your activity's\n[onCreate()](/reference/android/app/Activity#onCreate(android.os.Bundle)) method: \n\n### Kotlin\n\n```kotlin\nwindow.decorView.setOnSystemUiVisibilityChangeListener { visibility -\u003e\n // Note that system bars will only be \"visible\" if none of the\n // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.\n if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {\n // TODO: The system bars are visible. Make any desired\n // adjustments to your UI, such as showing the action bar or\n // other navigational controls.\n } else {\n // TODO: The system bars are NOT visible. Make any desired\n // adjustments to your UI, such as hiding the action bar or\n // other navigational controls.\n }\n}\n```\n\n### Java\n\n```java\nView decorView = getWindow().getDecorView();\ndecorView.setOnSystemUiVisibilityChangeListener\n (new View.OnSystemUiVisibilityChangeListener() {\n @Override\n public void onSystemUiVisibilityChange(int visibility) {\n // Note that system bars will only be \"visible\" if none of the\n // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.\n if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {\n // TODO: The system bars are visible. Make any desired\n // adjustments to your UI, such as showing the action bar or\n // other navigational controls.\n } else {\n // TODO: The system bars are NOT visible. Make any desired\n // adjustments to your UI, such as hiding the action bar or\n // other navigational controls.\n }\n }\n});\n```\n\nIt's generally good practice to keep your UI in sync with changes in system bar\nvisibility. For example, you could use this listener to hide and show the action bar in\nconcert with the status bar hiding and showing."]]