In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to adapt bangs in Android". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to adapt to bangs in Android.
1. Truncation of bottom UI caused by system downshift
The page number of the novel was truncated.
2. Bangs block the title bar and search box
Bangs block the title bar and search box
3. Abnormal display of PopupWindow
PopupWindow display exception
III. General adaptation scheme
Theoretically speaking, it is the most appropriate way to judge whether the phone is a banged screen phone through the bangs screen-related interface provided by Android P version, and to do some corresponding processing, but now it is not realistic to use the Android P interface in China, so it can only be adapted through the technical documents provided by major manufacturers, but the adaptation process is basically the same.
The adaptation process of bangs screen
The adaptation process of bangs screen
The key issues that need to be addressed are:
1. Whether the application has been adapted to bang Haiping
2. Whether the page displays a status bar
3.1 whether the application has been adapted to bang Haiping
At present, the mainstream models in China (Huawei, vivo, OPPO, Xiaomi) are divided into two camps in the display of bangs:
When the status bar is not displayed, the interface is displayed directly. "the previous position of the status bar is also used to display the interface", for example: OPPO
When the status bar is not displayed, directly "blacken the original position of the status bar and move the interface down as a whole", such as Huawei and vivo
Therefore, when we are adapting bangs, we first need to use some means to unify the display schemes of major manufacturers, so that all bangs phones use the interface of the status bar, and "inform the system" that we have adapted bangs to ensure that the system does not move down our applications and retain the native experience.
There are two main ways:
1. Set the aspect ratio of the screen.
Because the bangs' aspect ratio is larger than the previous phones, if it does not fit, Android defaults to a maximum aspect ratio of 1.86.
It is smaller than the aspect ratio of the bangs, so we need to declare a higher aspect ratio to tell the system that our application has adapted the bangs.
Just add the following configuration to AndroidManifest.xml:
You can also add attributes to Application:
Android:maxAspectRatio= "ratio_float"
Ps: this attribute requires API 26 to support
2. Set the application to support resize
We can also tell the system that we have adapted bangs by setting the app to support resizeable, and this is also the way officially recommended by Google. It is important to note, however, that after using this property, the application will also support split screen mode. Just add to the AndroidManifest.xml:
Android:resizeableActivity= "true"
3.2 whether the status bar is displayed on the page
For bangs screen adaptation, we divide the interface into two types:
For the interface with status bar, it will not be affected by bangs.
The interface with full screen display (without status bar) needs to move some controls down according to the display of the interface.
Therefore, we carry on the bangs screen adaptation, in fact, it is aimed at the interface without status bar, and the interface with status bar display is normal. For the interface without status bar, the main thing is to set the MarginTop corresponding to the bangs height for the controls obscured by bangs, so as to avoid the controls being obscured. For interfaces where the bottom may be truncated, consider making the bottom in the form of ScrollView.
Fourth, the adaptation scheme of each manufacturer
At present, the interface of Android P is not available, but all mobile phone manufacturers have developed their own API, so we need to make a special adaptation to each major model. Here we mainly introduce the adaptation schemes of the three mainstream mobile phones: vivo, OPPO and Huawei.
Huawei
Huawei, as a major domestic mobile phone manufacturer, has implemented an almost similar set of API by imitating the API provided by Android P, so if we want to tell the system that our application adapts to bangs, we'd better directly use Huawei's API, which is the safest.
The following code comes from: Huawei bangs screen adapts to official technical guidance.
1. Use bangs to display the application page setup
① scenario 1: add meta-data attribute to AndroidManifest.xml, which can take effect not only for Application but also for Activity configuration:
After adding this attribute, the system will move the application down to ensure the native experience.
② solution 2: set the interface to use bangs by adding window FLAG:
Public static void setFullScreenWindowLayoutInDisplayCutout (Window window) {if (window = = null) {return;} WindowManager.LayoutParams layoutParams = window.getAttributes (); try {Class layoutParamsExCls = Class.forName ("com.huawei.android.view.LayoutParamsEx"); Constructor con=layoutParamsExCls.getConstructor (LayoutParams.class); Object layoutParamsExObj=con.newInstance (layoutParams); Method method=layoutParamsExCls.getMethod ("addHwFlags", int.class); method.invoke (layoutParamsExObj, FLAG_NOTCH_SUPPORT) } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {Log.e ("test", "hw add notch screen flag api error");} catch (Exception e) {Log.e ("test", "other Exception");}}
2. Judge whether the Huawei phone is bangs or not.
Public static boolean hasNotchInHuawei (Context context) {boolean hasNotch = false; try {ClassLoader cl = context.getClassLoader (); Class HwNotchSizeUtil = cl.loadClass ("com.huawei.android.util.HwNotchSizeUtil"); Method hasNotchInScreen = HwNotchSizeUtil.getMethod ("hasNotchInScreen"); if (hasNotchInScreen! = null) {hasNotch = (boolean) hasNotchInScreen.invoke (HwNotchSizeUtil);} catch (Exception e) {e.printStackTrace ();} return hasNotch;}
3. Get the height of bangs
Public static int [] getNotchSize (Context context) {int [] ret = new int [] {0,0}; try {ClassLoader cl = context.getClassLoader (); Class HwNotchSizeUtil = cl.loadClass ("com.huawei.android.util.HwNotchSizeUtil"); Method get = HwNotchSizeUtil.getMethod ("getNotchSize"); ret = (int []) get.invoke (HwNotchSizeUtil);} catch (ClassNotFoundException e) {Log.e ("test", "getNotchSize ClassNotFoundException") } catch (NoSuchMethodException e) {Log.e ("test", "getNotchSize NoSuchMethodException");} catch (Exception e) {Log.e ("test", "getNotchSize Exception");} finally {return ret;}
OPPO
OPPO is a clean stream among mainstream manufacturers, learning from iPhoneX is the most similar. For the interface that does not display the status bar, the OPPO phone adopts the solution of "the original position of the status bar is also used to display the interface", so we just need to move the position of the relevant controls.
The following code comes from: OPPO concave screen adaptation instructions
1. Judge whether the OPPO phone is a bangs phone.
Public static boolean hasNotchInOppo (Context context) {return context.getPackageManager () .hasSystemFeature ("com.oppo.feature.screen.heteromorphism");
2. Get the height of bangs
For the bangs height of OPPO bangs, the OPPO official documentation does not provide the relevant API, but the official documents show that the bangs height of OPPO phones is the same as the height of the status bar, and I have verified it, and it is true. So we can directly get the height of the status bar as the bang height of the OPPO phone.
Public static int getStatusBarHeight (Context context) {int statusBarHeight = 0; int resourceId = context.getResources (). GetIdentifier ("status_bar_height", "dimen", "android"); if (resourceId > 0) {statusBarHeight = context.getResources () .getDimensionPixelSize (resourceId);} return statusBarHeight;}
Vivo
The technical documentation provided by vivo is the least friendly for developers. It only provides an API to judge the bangs, but does not provide a way to obtain the bangs height. We can only use the height of the status bar as the bangs height, but there may be some deviation in some models.
Official documentation: vivo Mobile phone adaptation Guide
Determine whether the vivo phone is a bangs phone.
Public static boolean hasNotchInVivo (Context context) {boolean hasNotch = false; try {ClassLoader cl = context.getClassLoader (); Class ftFeature = cl.loadClass ("android.util.FtFeature"); Method [] methods = ftFeature.getDeclaredMethods (); if (methods! = null) {for (int I = 0; I < methods.length; iTunes +) {Method method = methods [I] If (method! = null) {if (method.getName (). EqualsIgnoreCase ("isFeatureSupport")) {hasNotch = (boolean) method.invoke (ftFeature, 0x00000020); break;}} catch (Exception e) {e.printStackTrace (); hasNotch = false;} return hasNotch } at this point, I believe you have a deeper understanding of "how to fit bangs in Android". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.